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;
}
}

浙公网安备 33010602011771号