Introduction
In a lot of problems, we are asked to reverse the links between a set of nodes of a LinkedList. Often, the constraint is that we need to do this in-place, i.e., using the existing node objects and without using extra memory.
In-place Reversal of a LinkedList pattern describes an efficient way to solve the above problem. In the following chapters, we will solve a bunch of problems using this pattern.
Let’s jump on to our first problem to understand this pattern.
Reverse a LinkedList (easy)
206. Reverse Linked List Design Gurus Educative.io
Problem Statement
Given the head of a Singly LinkedList, reverse the LinkedList. Write a function to return the new head of the reversed LinkedList.
Constraints:
- The number of nodes in the list is the range
[0, 5000]
. -5000 <= Node.val <= 5000
Solution
To reverse a LinkedList, we need to reverse one node at a time. We will start with a variable current
which will initially point to the head of the LinkedList and a variable previous
which will point to the previous node that we have processed; initially previous
will point to null
.
In a stepwise manner, we will reverse the current
node by pointing it to the previous
before moving on to the next node. Also, we will update the previous
to always point to the previous node that we have processed. Here is the visual representation of our algorithm:
Code
Here is what our algorithm will look like:
1 | # class Node: |
Time complexity
The time complexity of our algorithm will be O(N) where ‘N’ is the total number of nodes in the LinkedList.
Space complexity
We only used constant space, therefore, the space complexity of our algorithm is O(1).
*Reverse a Sub-list (medium)
Top Interview 150 | 92. Reverse Linked List II Design Gurus Educative.io
Problem Statement
Given the head of a LinkedList and two positions ‘p’ and ‘q’, reverse the LinkedList from position ‘p’ to ‘q’.
Constraints:
- The number of nodes in the list is n.
1 <= n <= 500
-500 <= Node.val <= 500
1 <= left <= right <= n
Solution
The problem follows the In-place Reversal of a LinkedList pattern. We can use a similar approach as discussed in Reverse a LinkedList. Here are the steps we need to follow:
- Skip the first
p-1
nodes, to reach the node at positionp
. - Remember the node at position
p-1
to be used later to connect with the reversed sub-list. - Next, reverse the nodes from
p
toq
using the same approach discussed in Reverse a LinkedList. - Connect the
p-1
andq+1
nodes to the reversed sub-list. - 思路和我的是一样的,比较好奇他是怎么解决P == 1的情况的
Code
Here is what our algorithm will look like:
1 | class Node: |
Time complexity
The time complexity of our algorithm will be O(N) where ‘N’ is the total number of nodes in the LinkedList.
Space complexity
We only used constant space, therefore, the space complexity of our algorithm is O(1).
Similar Questions
Problem 1: Reverse the first ‘k’ elements of a given LinkedList.
Solution: This problem can be easily converted to our parent problem; to reverse the first ‘k’ nodes of the list, we need to pass p=1
and q=k
.
Problem 2: Given a LinkedList with ‘n’ nodes, reverse it based on its size in the following way:
- If ‘n’ is even, reverse the list in a group of n/2 nodes.
- If n is odd, keep the middle node as it is, reverse the first ‘n/2’ nodes and reverse the last ‘n/2’ nodes.
Solution: When ‘n’ is even we can perform the following steps:
- Reverse first ‘n/2’ nodes:
head = reverse(head, 1, n/2)
- Reverse last ‘n/2’ nodes:
head = reverse(head, n/2 + 1, n)
When ‘n’ is odd, our algorithm will look like:
head = reverse(head, 1, n/2)
head = reverse(head, n/2 + 2, n)
Please note the function call in the second step. We’re skipping two elements as we will be skipping the middle element.
*Reverse every K-element Sub-list (medium)
Similar |Top Interview 150 | 25. Reverse Nodes in k-Group Design Gurus Educative.io
Problem Statement
Given the head of a LinkedList and a number ‘k’, reverse every ‘k’ sized sub-list starting from the head.
If, in the end, you are left with a sub-list with less than ‘k’ elements, reverse it too.
Constraints:
- The number of nodes in the list is n.
1 <= k <= n <= 5000
0 <= Node.val <= 1000
Solution
The problem follows the In-place Reversal of a LinkedList pattern and is quite similar to Reverse a Sub-list. 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’.
Code
Most of the code is the same as Reverse a Sub-list; only the highlighted lines have a majority of the changes:
1 | # class Node: |
Time complexity
The time complexity of our algorithm will be O(N) where ‘N’ is the total number of nodes in the LinkedList.
Space complexity
We only used constant space, therefore, the space complexity of our algorithm is O(1).
*Problem Challenge 1
Design Gurus Educative.io
Reverse alternating K-element Sub-list (medium)
Given the head of a LinkedList and a number ‘k’, reverse every alternating ‘k’ sized sub-list starting from the head.
If, in the end, you are left with a sub-list with less than ‘k’ elements, reverse it too.
Constraints:
- The number of nodes in the list is n.
1 <= k <= n <= 5000
0 <= Node.val <= 1000
Solution
The problem follows the In-place Reversal of a LinkedList pattern and is quite similar to Reverse every K-element Sub-list. 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.
Code
Most of the code is the same as Reverse every K-element Sub-list; only the highlighted lines have a majority of the changes:
1 | # class Node: |
Time complexity
The time complexity of our algorithm will be O(N) where ‘N’ is the total number of nodes in the LinkedList.
Space complexity
We only used constant space, therefore, the space complexity of our algorithm is O(1).
Problem Challenge 2
Top Interview 150 | 61. Rotate List Design Gurus Educative.io
Rotate a LinkedList (medium)
Given the head of a Singly LinkedList and a number ‘k’, rotate the LinkedList to the right by ‘k’ nodes.
Solution
Another way of defining the rotation is to take the sub-list of ‘k’ ending nodes of the LinkedList and connect them to the beginning. Other than that we have to do three more things:
- Connect the last node of the LinkedList to the head, because the list will have a different tail after the rotation.
- The new head of the LinkedList will be the node at the beginning of the sublist.
- The node right before the start of sub-list will be the new tail of the rotated LinkedList.
Code
Here is what our algorithm will look like:
1 | # class Node: |
Time complexity
The time complexity of our algorithm will be O(N) where ‘N’ is the total number of nodes in the LinkedList.
Space complexity
We only used constant space, therefore, the space complexity of our algorithm is O(1).