206. Reverse Linked List
Reverse a singly 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)
return head;
ListNode pReverseHead=null;
ListNode pPre=null;
ListNode pNode=head;
while(pNode!=null){
ListNode pPost=pNode.next;
if(pPost==null)
pReverseHead=pNode;
pNode.next=pPre;
pPre=pNode;
pNode=pPost;
}
return pReverseHead;
}
}