Skip to content

Commit 5d47025

Browse files
committed
reverse linklist
1 parent 558afb3 commit 5d47025

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Source : https://leetcode.com/problems/reverse-linked-list/
2+
// Author : henrytien
3+
// Date : 2022-02-10
4+
5+
/*****************************************************************************************************
6+
*
7+
* Given the head of a singly linked list, reverse the list, and return the reversed list.
8+
*
9+
* Example 1:
10+
*
11+
* Input: head = [1,2,3,4,5]
12+
* Output: [5,4,3,2,1]
13+
*
14+
* Example 2:
15+
*
16+
* Input: head = [1,2]
17+
* Output: [2,1]
18+
*
19+
* Example 3:
20+
*
21+
* Input: head = []
22+
* Output: []
23+
*
24+
* Constraints:
25+
*
26+
* The number of nodes in the list is the range [0, 5000].
27+
* -5000 <= Node.val <= 5000
28+
*
29+
* Follow up: A linked list can be reversed either iteratively or recursively. Could you implement
30+
* both?
31+
******************************************************************************************************/
32+
33+
class Solution1
34+
{
35+
public:
36+
ListNode *reverseList(ListNode *head)
37+
{
38+
if (head == NULL)
39+
{
40+
return head;
41+
}
42+
43+
ListNode *curr = head;
44+
ListNode *res = NULL;
45+
while (curr != NULL)
46+
{
47+
ListNode *temp = curr->next;
48+
curr->next = res;
49+
res = curr;
50+
curr = temp;
51+
}
52+
return res;
53+
}
54+
};
55+
56+
class Solution {
57+
public:
58+
ListNode* reverseList(ListNode* head) {
59+
if (head == NULL || head->next == NULL) return head;
60+
ListNode* p = reverseList(head->next);
61+
head->next->next = head;
62+
head->next = NULL;
63+
return p;
64+
}
65+
};

Diff for: leetcode/206.reverse_linked_list/README.md

+101
Original file line numberDiff line numberDiff line change
@@ -1 +1,102 @@
11
# [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

Comments
 (0)