|
| 1 | +# Python program to rotate a linked list |
| 2 | + |
| 3 | +# Node class |
| 4 | +class Node: |
| 5 | + |
| 6 | + # Constructor to initialize the node object |
| 7 | + def __init__(self, data): |
| 8 | + self.data = data |
| 9 | + self.next = None |
| 10 | + |
| 11 | +class LinkedList: |
| 12 | + |
| 13 | + # Function to initialize head |
| 14 | + def __init__(self): |
| 15 | + self.head = None |
| 16 | + |
| 17 | + # Function to insert a new node at the beginning |
| 18 | + def push(self, new_data): |
| 19 | + # allocate node and put the data |
| 20 | + new_node = Node(new_data) |
| 21 | + |
| 22 | + # Make next of new node as head |
| 23 | + new_node.next = self.head |
| 24 | + |
| 25 | + # move the head to point to the new Node |
| 26 | + self.head = new_node |
| 27 | + |
| 28 | + # Utility function to print it the linked LinkedList |
| 29 | + def printList(self): |
| 30 | + temp = self.head |
| 31 | + while(temp): |
| 32 | + print temp.data, |
| 33 | + temp = temp.next |
| 34 | + |
| 35 | + # This function rotates a linked list counter-clockwise and |
| 36 | + # updates the head. The function assumes that k is smaller |
| 37 | + # than size of linked list. It doesn't modify the list if |
| 38 | + # k is greater than of equal to size |
| 39 | + def rotate(self, k): |
| 40 | + if k == 0: |
| 41 | + return |
| 42 | + |
| 43 | + # Let us understand the below code for example k = 4 |
| 44 | + # and list = 10->20->30->40->50->60 |
| 45 | + current = self.head |
| 46 | + |
| 47 | + # current will either point to kth or NULL after |
| 48 | + # this loop |
| 49 | + # current will point to node 40 in the above example |
| 50 | + count = 1 |
| 51 | + while(count <k and current is not None): |
| 52 | + current = current.next |
| 53 | + count += 1 |
| 54 | + |
| 55 | + # If current is None, k is greater than or equal |
| 56 | + # to count of nodes in linked list. Don't change |
| 57 | + # the list in this case |
| 58 | + if current is None: |
| 59 | + return |
| 60 | + |
| 61 | + # current points to kth node. Store it in a variable |
| 62 | + # kth node points to node 40 in the above example |
| 63 | + kthNode = current |
| 64 | + |
| 65 | + # current will point to lsat node after this loop |
| 66 | + # current will point to node 60 in above example |
| 67 | + while(current.next is not None): |
| 68 | + current = current.next |
| 69 | + |
| 70 | + # Change next of last node to previous head |
| 71 | + # Next of 60 is now changed to node 10 |
| 72 | + current.next = self.head |
| 73 | + |
| 74 | + # Change head to (k + 1)th node |
| 75 | + # head is not changed to node 50 |
| 76 | + self.head = kthNode.next |
| 77 | + |
| 78 | + # change next of kth node to NULL |
| 79 | + # next of 40 is not NULL |
| 80 | + kthNode.next = None |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +# Driver program to test above function |
| 85 | +llist = LinkedList() |
| 86 | + |
| 87 | +# Create a list 10->20->30->40->50->60 |
| 88 | +for i in range(60, 0, -10): |
| 89 | + llist.push(i) |
| 90 | + |
| 91 | +print "Given linked list" |
| 92 | +llist.printList() |
| 93 | +llist.rotate(4) |
| 94 | + |
| 95 | +print "\nRotated Linked list" |
| 96 | +llist.printList() |
| 97 | + |
| 98 | +# This code is contributed by Mehraj |
0 commit comments