Reverse a linked list. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL
,
return 5->4->3->2->1->NULL
.
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode pre = head;
ListNode cur = head.next;
ListNode pos = cur.next;
pre.next = null;
while (pos != null) {
cur.next = pre;
pre = cur;
cur = pos;
pos = pos.next;
}
cur.next = pre;
return cur;
}