|
1 | 1 | # [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)
|
| 2 | + |
| 3 | +## Solution |
| 4 | + |
| 5 | +The solution from [leetcode](https://leetcode.com/problems/reverse-linked-list/solution/). |
| 6 | + |
| 7 | +------ |
| 8 | + |
| 9 | +#### Approach #1 (Iterative) [Accepted] |
| 10 | + |
| 11 | +Assume that we have linked list `1 → 2 → 3 → Ø`, we would like to change it to `Ø ← 1 ← 2 ← 3`. |
| 12 | + |
| 13 | +While you are traversing the list, change the current node's next pointer to point to its previous element. Since a node does not have reference to its previous node, you must store its previous element beforehand. You also need another pointer to store the next node before changing the reference. Do not forget to return the new head reference at the end! |
| 14 | + |
| 15 | +```c++ |
| 16 | +class Solution |
| 17 | +{ |
| 18 | +public: |
| 19 | + ListNode *reverseList(ListNode *head) |
| 20 | + { |
| 21 | + if (head == NULL) |
| 22 | + { |
| 23 | + return head; |
| 24 | + } |
| 25 | + |
| 26 | + ListNode *curr = head; |
| 27 | + ListNode *res = NULL; |
| 28 | + while (curr != NULL) |
| 29 | + { |
| 30 | + ListNode *temp = curr->next; |
| 31 | + curr->next = res; |
| 32 | + res = curr; |
| 33 | + curr = temp; |
| 34 | + } |
| 35 | + return res; |
| 36 | + } |
| 37 | +}; |
| 38 | +``` |
| 39 | + |
| 40 | +**Complexity analysis** |
| 41 | + |
| 42 | +- Time complexity : O(n)*O*(*n*). Assume that n*n* is the list's length, the time complexity is O(n)*O*(*n*). |
| 43 | +- Space complexity : O(1)*O*(1). |
| 44 | + |
| 45 | +#### Approach #2 (Recursive) [Accepted] |
| 46 | + |
| 47 | +The recursive version is slightly trickier and the key is to work backwards. Assume that the rest of the list had already been reversed, now how do I reverse the front part? Let's assume the list is: n1 → … → nk-1 → nk → nk+1 → … → nm → Ø |
| 48 | + |
| 49 | +Assume from node nk+1 to nm had been reversed and you are at node nk. |
| 50 | + |
| 51 | +n1 → … → nk-1 → **nk** → nk+1 ← … ← nm |
| 52 | + |
| 53 | +We want nk+1’s next node to point to nk. |
| 54 | + |
| 55 | +So, |
| 56 | + |
| 57 | +nk.next.next = nk; |
| 58 | + |
| 59 | +Be very careful that n1's next must point to Ø. If you forget about this, your linked list has a cycle in it. This bug could be caught if you test your code with a linked list of size 2. |
| 60 | + |
| 61 | +```c++ |
| 62 | +class Solution { |
| 63 | +public: |
| 64 | + ListNode* reverseList(ListNode* head) { |
| 65 | + if (head == NULL || head->next == NULL) return head; |
| 66 | + ListNode* p = reverseList(head->next); |
| 67 | + head->next->next = head; |
| 68 | + head->next = NULL; |
| 69 | + return p; |
| 70 | + } |
| 71 | +}; |
| 72 | +``` |
| 73 | +
|
| 74 | +**Complexity analysis** |
| 75 | +
|
| 76 | +- Time complexity : O(n)*O*(*n*). Assume that n*n* is the list's length, the time complexity is O(n)*O*(*n*). |
| 77 | +- Space complexity : O(n)*O*(*n*). The extra space comes from implicit stack space due to recursion. The recursion could go up to n*n* levels deep. |
| 78 | +
|
| 79 | +
|
| 80 | +
|
| 81 | +> List: 1 --> 2 --> 3 --> 4 --> NULL |
| 82 | +> 1st recursion: head = 1, p = ? |
| 83 | +> 2nd recursion: head = 2, p = ? |
| 84 | +> 3rd recursion: head = 3, p = ? |
| 85 | +> 4th recursion: |
| 86 | +> head = 4, since head->next == NULL, return head |
| 87 | +> |
| 88 | +> |
| 89 | +> |
| 90 | +> Back to the 3rd recursion: |
| 91 | +> head = 3, p = 4 |
| 92 | +> |
| 93 | +> |
| 94 | +> |
| 95 | +> head->next->next = head; we can understand it part by part, |
| 96 | +> |
| 97 | +> |
| 98 | +> |
| 99 | +> head->next is 4 |
| 100 | +> head->next->next = head is like 4->next = 3, set value of 4 point to equal to 3, so 4 --> 3 |
| 101 | +> head->next = null; 4 --> 3 --> NULL |
| 102 | +> return p; |
0 commit comments