Reverse Linked List

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null)
return head;

ListNode p1 = head.next;
head.next = null;
ListNode last = null;
do
{
last = p1.next;

p1.next = head;
head = p1;
p1 = last;

}while (last!=null);

return head;
}
}

posted @ 2015-08-24 22:51  新一代的天皇巨星  阅读(128)  评论(0)    收藏  举报