Javatpoint Logo
Javatpoint Logo

Rotate a Linked List in Python

If given the head of a singly linked list and a number K, develop a program that rotates the linked list clockwise by K places starting from the last node.

Example

Input-1

Output 1:

40 -> 50 -> 10 -> 20 -> 30  

Explanation

We rotated the given linked list exactly twice in this case. During the initial rotation, 5 will be the head node, and 4 will be the tail node, whereas on rotating the list a second time, 4 will be the head node, and 3 will be the tail node.

Input-2

Output 2:

30 -> 10 -> 20

Explanation

Here, we have performed 4 rotations of the linked list.

Algorithm 1 - Rotating by Changing the Next Pointer for the kth Node

Intuition:

For this strategy, we must keep three-pointers to the (k + 1)th node, the kth node, and the node that is previous to the kth node. The goal is to traverse the given linked list till the kth node and set the node present at (k + 1)th pointer to NULL, and the next of the last node of the linked list should store the current head node, after which the (k + 1)th node will become the new head of our linked list.

Algorithm

  1. Check if the given head is null or if k == 0. If yes, then we will return the linked If yes, then we will return the linked list.
  2. Initialize the variable c to 1 and initialize another variable containing the total number of nodes present in the linked list (for head).
  3. Traverse the given linked list until you reach the kth node.
  4. Check if the current node is NULL or kth is more than or equal to the total number of nodes present in the linked list. In this instance, don't update the list; therefore, return.
  5. Now go through all the nodes until you reach the end and update the next of the last node to store the previous head.
  6. Lastly, shift the head to the (k + 1)th node, followed by setting the (k + 1)th node to None.

Code Implementation

Code

Output:

The original linked list is:
1 4 5 5 7 2 1 5 3 9

The rotated Linked list is:
5 5 7 2 1 5 3 9 1 4

Time Complexity: The time complexity of this approach is O(n). This is because we have traversed the given linked list one time to rotate the linked list, where n is the number of nodes in the linked list.

Space Complexity: The space complexity of this approach is O(1). It is constant because no extra space is acquired in the program.

Algorithm 2 - By Rotating the K Number of Nodes

Intuition:

The approach here is analogous to the last one. In this, the single linked list is converted to something like a circular linked list and afterward moved k-1 steps ahead from the head node, but before shifting, make the (k - 1)th node next to null and the following node as head.

Algorithm

  1. Determine if the head node is null or if k == 0. If so, the return head.
  2. Point the next of the last node toward the head, transforming the single linked list into a circular linked list.
  3. Navigate this linked list to the (k - 1)th place, which will be the last entry.
  4. Iterate through the last node of the circular linked list and break the loop by referring the next of the last node to None.

Code Implementation

Code

Output:

The original List is:
1 2 3 4 5 6 7 8
The rotated linked list is:
6 7 8 1 2 3 4 5

Time Complexity: The time complexity of this approach is O(n). This is because we have traversed the given linked list one time to rotate the linked list, where n is the number of nodes in the linked list.

Space Complexity: The space complexity of this approach is O(1). It is constant because no extra space is acquired in the program.

Conclusion

  1. We will give a single linked list's head and K. We had to develop a program that rotated the given Linked List by the Kth node clockwise.
  2. For example, if a linked list's head is specified as Head: 1 -> 2 -> 3 -> 4 -> 5 and the integer K = 3, the result should be 5 -> 1 -> 2 -> 3 -> 4 after three rotations.
  3. We learned two methods for rotating any given linked list: the first is changing the next for each kth node, and the second is rotating the K nodes.
  4. Since we only traversed our linked list once, both the methods have a time complexity of O(n) and an O(1) space complexity.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA