|
1 | 1 | # Pattern 6: In-place Reversal of a LinkedList
|
2 | 2 |
|
3 |
| -In a lot of problems, we are asked to reverse the links between a set of nodes of a <b>LinkedList</b>. Often, the constraint is that we need to do this in-place, i.e., using the existing node objects and without using extra memory. |
| 3 | +In a lot of problems, we are asked to reverse the links between a set of nodes of a <b>LinkedList</b>. Often, the constraint is that we need to do this </i>in-place</i>, i.e., using the existing node objects and without using extra memory. |
4 | 4 |
|
5 |
| -<b>In-place Reversal of a LinkedList pattern</b> describes an efficient way to solve the above problem. |
| 5 | +<b></i>in-place</i> Reversal of a LinkedList pattern</b> describes an efficient way to solve the above problem. |
6 | 6 |
|
7 | 7 | ## Reverse a LinkedList (easy)
|
8 | 8 | https://leetcode.com/problems/reverse-linked-list/
|
@@ -62,14 +62,14 @@ console.log(`Nodes of original LinkedList are: ${head.printList()}`)
|
62 | 62 | console.log(`Nodes of reversed LinkedList are: ${reverse(head).printList()}`)
|
63 | 63 | ````
|
64 | 64 |
|
65 |
| -- The time complexity of our algorithm will be `O(N)` where `N` is the total number of nodes in the <b>LinkedList</b>. |
| 65 | +- The time complexity of our algorithm will be `O(N)` where `N’` is the total number of nodes in the <b>LinkedList</b>. |
66 | 66 | - We only used constant space, therefore, the space complexity of our algorithm is `O(1)`.
|
67 | 67 |
|
68 | 68 | ## Reverse a Sub-list (medium)
|
69 | 69 | https://leetcode.com/problems/reverse-linked-list-ii/
|
70 | 70 | > Given the head of a <b>LinkedList</b> and two positions `p` and `q`, reverse the <b>LinkedList</b> from position `p` to `q`.
|
71 | 71 |
|
72 |
| -The problem follows the <b>In-place Reversal</b> of a <b>LinkedList</b> pattern. We can use a similar approach as discussed in <b>Reverse a LinkedList</b>. Here are the steps we need to follow: |
| 72 | +The problem follows the <b></i>in-place</i> Reversal</b> of a <b>LinkedList</b> pattern. We can use a similar approach as discussed in <b>Reverse a LinkedList</b>. Here are the steps we need to follow: |
73 | 73 | 1. Skip the first `p-1` nodes, to reach the node at position `p`.
|
74 | 74 | 2. Remember the node at position `p-1` to be used later to connect with the reversed sub-list.
|
75 | 75 | 3. Next, reverse the nodes from `p` to `q` using the same approach discussed in <b>Reverse a LinkedList</b>.
|
@@ -187,7 +187,7 @@ https://leetcode.com/problems/reverse-nodes-in-k-group/
|
187 | 187 | > Given the head of a <b>LinkedList</b> and a number ‘k’, <b>reverse every ‘k’ sized sub-list</b> starting from the head.
|
188 | 188 | > If, in the end, you are left with a sub-list with less than ‘k’ elements, reverse it too.
|
189 | 189 |
|
190 |
| -The problem follows the <b>In-place Reversal of a LinkedList</b> pattern and is quite similar to <b>Reverse a Sub-list</b>. The only difference is that we have to reverse all the sub-lists. We can use the same approach, starting with the first sub-list (i.e. `p=1, q=k`) and keep reversing all the sublists of size ‘k’. |
| 190 | +The problem follows the <b></i>in-place</i> Reversal of a LinkedList</b> pattern and is quite similar to <b>Reverse a Sub-list</b>. The only difference is that we have to reverse all the sub-lists. We can use the same approach, starting with the first sub-list (i.e. `p=1, q=k`) and keep reversing all the sublists of size ‘k’. |
191 | 191 |
|
192 | 192 | ````js
|
193 | 193 | class Node {
|
@@ -275,7 +275,7 @@ console.log(`Nodes of reversed LinkedList are: ${reverseEveryKElements(head, 3).
|
275 | 275 | >
|
276 | 276 | > If, in the end, you are left with a sub-list with less than `K` elements, reverse it too.
|
277 | 277 |
|
278 |
| -The problem follows the <b>In-place Reversal of a LinkedList</b> pattern and is quite similar to <b>Reverse every K-element Sub-list</b>. The only difference is that we have to skip `K` alternating elements. We can follow a similar approach, and in each iteration after reversing `K` elements, we will skip the next `K` elements. |
| 278 | +The problem follows the <b></i>in-place</i> Reversal of a LinkedList</b> pattern and is quite similar to <b>Reverse every K-element Sub-list</b>. The only difference is that we have to skip `K` alternating elements. We can follow a similar approach, and in each iteration after reversing `K` elements, we will skip the next `K` elements. |
279 | 279 |
|
280 | 280 | ````class Node {
|
281 | 281 | constructor(value, next = null) {
|
@@ -359,7 +359,7 @@ process.stdout.write('Nodes of reversed LinkedList are: ');
|
359 | 359 | result.printList();
|
360 | 360 | ````
|
361 | 361 |
|
362 |
| -- The time complexity of our algorithm will be `O(N)`where `N` is the total number of nodes in the <b>LinkedList</b>. |
| 362 | +- The time complexity of our algorithm will be `O(N)`where `N’` is the total number of nodes in the <b>LinkedList</b>. |
363 | 363 | - We only used constant space, therefore, the space complexity of our algorithm is `O(1)`.
|
364 | 364 | ## 🌟 Rotate a LinkedList (medium)
|
365 | 365 | https://leetcode.com/problems/rotate-list/
|
@@ -433,5 +433,5 @@ head.next.next.next.next.next = new Node(6)
|
433 | 433 | console.log(`Nodes of original LinkedList are: ${head.getList()}`)
|
434 | 434 | console.log(`Nodes of rotated LinkedList are: ${rotate(head, 3).getList()}`)
|
435 | 435 | ````
|
436 |
| -- The time complexity of our algorithm will be `O(N)` where `N` is the total number of nodes in the <b>LinkedList</b>. |
| 436 | +- The time complexity of our algorithm will be `O(N)` where `N’` is the total number of nodes in the <b>LinkedList</b>. |
437 | 437 | - We only used constant space, therefore, the space complexity of our algorithm is `O(1)`.
|
0 commit comments