Question
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Solution
1 2 3 4 5 6 7 8 9 10 11 12
| public ListNode reverseList(ListNode head) { if (head == null) return null; ListNode pre = null, cnt = head; while (cnt != null) { ListNode tmp = cnt.next; cnt.next = pre; pre = cnt; cnt = tmp; } return pre; }
|