You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -19,7 +19,7 @@ Reverse it in-place and in one-pass
19
19
20
20
It would be much easier to reverse an array than a linked list, since array supports random access with index, while singly linked list can ONLY be operated through its head node. So an approach without index is required.
21
21
22
-
Think about how can '1->2->3' become '3->2->1'. Starting from '1', we should turn '1->2' into '2->1', then '2->3' into '3->2', and so on. The key is how to swap two adjacent nodes.
22
+
Think about how '1->2->3' can become '3->2->1'. Starting from '1', we should turn '1->2' into '2->1', then '2->3' into '3->2', and so on. The key is how to swap two adjacent nodes.
23
23
24
24
```
25
25
temp = head -> next;
@@ -128,17 +128,17 @@ Already covered in the solution part. One more word, the assignment of `prev` is
128
128
129
129
### Complexity
130
130
131
-
Traversing the linked list, so the time complexity is $$O(n)$$. $$O(1)$$auxiliary space complexity.
131
+
Traversing the linked list leads to ***O(n)*** time complexity, and auxiliary space complexity is ***O(1)***.
132
132
133
133
## Solution2 - Recursively
134
134
135
135
Three cases when the recursion ceases:
136
136
137
137
1. If given linked list is null, just return.
138
138
2. If given linked list has only one node, return that node.
139
-
3. If given linked list has at least two nodes, pick out the head node and regard the following nodes as a whole entity, swap them, then recurse that entity.
139
+
3. If given linked list has at least two nodes, pick out the head node and regard the following nodes as a sub-linked-list, swap them, then recurse that sub-linked-list.
140
140
141
-
Be careful when swapping the head node (refer as `Node0`) and head of the following nodes entity (refer as 'Node1' ): First, swap `Node0` and `Node1`; Second, assign `null` to `Node0`'s next (or it would fall into infinite loop, and tail of result list won't point to `null`).
141
+
Be careful when swapping the head node (refer as `nodeY`) and head of the sub-linked-list (refer as 'nodeX' ): First, swap `nodeY` and `nodeX`; Second, assign `null` to `nodeY->next` (or it would fall into infinite loop, and tail of result list won't point to `null`).
0 commit comments