Python Program For Rotating A Linked List
Last Updated :
18 May, 2022
Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smaller than the count of nodes in a linked list.
Method 1:
To rotate the linked list, we need to change the next of kth node to NULL, the next of the last node to the previous head node, and finally, change the head to (k+1)th node. So we need to get hold of three nodes: kth node, (k+1)th node, and last node.Â
Traverse the list from the beginning and stop at kth node. Store pointer to kth node. We can get (k+1)th node using kthNode->next. Keep traversing till the end and store a pointer to the last node also. Finally, change pointers as stated above.
Below image shows how to rotate function works in the code :
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def printList( self ):
temp = self .head
while (temp):
print temp.data,
temp = temp. next
def rotate( self , k):
if k = = 0 :
return
current = self .head
count = 1
while (count <k and
current is not None ):
current = current. next
count + = 1
if current is None :
return
kthNode = current
while (current. next is not None ):
current = current. next
current. next = self .head
self .head = kthNode. next
kthNode. next = None
llist = LinkedList()
for i in range ( 60 , 0 , - 10 ):
llist.push(i)
print "Given linked list"
llist.printList()
llist.rotate( 4 )
print "Rotated Linked list"
llist.printList()
|
p>
Output:Â
Given linked list
10 20 30 40 50 60
Rotated Linked list
50 60 10 20 30 40
Time Complexity: O(n) where n is the number of nodes in Linked List. The code traverses the linked list only once.
Method 2:
To rotate a linked list by k, we can first make the linked list circular and then moving k-1 steps forward from head node, making (k-1)th node’s next to null and make kth node as head.
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
def rotate(head_ref, k):
if (k = = 0 ):
return
current = head_ref
while (current. next ! = None ):
current = current. next
current. next = head_ref
current = head_ref
for i in range (k - 1 ):
current = current. next
head_ref = current. next
current. next = None
return head_ref
def push(head_ref, new_data):
new_node = Node()
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
def printList(node):
while (node ! = None ):
print (node.data, end = ' ' )
node = node. next
if __name__ = = '__main__' :
head = None
for i in range ( 60 , 0 , - 10 ):
head = push(head, i)
print ( "Given linked list " )
printList(head)
head = rotate(head, 4 )
print ( "\nRotated Linked list " )
printList(head)
|
Output:
Given linked list
10 20 30 40 50 60
Rotated Linked list
50 60 10 20 30 40
Please refer complete article on Rotate a Linked List for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...