Python Program For Reversing A Linked List In Groups Of Given Size- Set 2 Last Updated : 18 May, 2022 Comments Improve Suggest changes Like Article Like Report Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples: Input: 1->2->3->4->5->6->7->8->NULL and k = 3 Output: 3->2->1->6->5->4->8->7->NULL. Input: 1->2->3->4->5->6->7->8->NULL and k = 5 Output: 5->4->3->2->1->8->7->6->NULL. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. We have already discussed its solution in below post Reverse a Linked List in groups of given size | Set 1In this post, we have used a stack which will store the nodes of the given linked list. Firstly, push the k elements of the linked list in the stack. Now pop elements one by one and keep track of the previously popped node. Point the next pointer of prev node to top element of stack. Repeat this process, until NULL is reached.This algorithm uses O(k) extra space. Python3 # Python3 program to reverse a Linked List # in groups of given size # Node class class Node(object): __slots__ = 'data', 'next' # Constructor to initialize the # node object def __init__(self, data = None, next = None): self.data = data self.next = next def __repr__(self): return repr(self.data) class LinkedList(object): # Function to initialize head def __init__(self): self.head = None # Utility function to print # nodes of LinkedList def __repr__(self): nodes = [] curr = self.head while curr: nodes.append(repr(curr)) curr = curr.next return '[' + ', '.join(nodes) + ']' # Function to insert a new node at # the beginning def prepend(self, data): self.head = Node(data = data, next = self.head) # Reverses the linked list in groups # of size k and returns the pointer # to the new head node. def reverse(self, k = 1): if self.head is None: return curr = self.head prev = None new_stack = [] while curr is not None: val = 0 # Terminate the loop whichever # comes first either current == None # or value >= k while curr is not None and val < k: new_stack.append(curr.data) curr = curr.next val += 1 # Now pop the elements of stack one # by one while new_stack: # If final list has not been # started yet. if prev is None: prev = Node(new_stack.pop()) self.head = prev else: prev.next = Node(new_stack.pop()) prev = prev.next # Next of last element will point to None. prev.next = None return self.head # Driver Code llist = LinkedList() llist.prepend(9) llist.prepend(8) llist.prepend(7) llist.prepend(6) llist.prepend(5) llist.prepend(4) llist.prepend(3) llist.prepend(2) llist.prepend(1) print("Given linked list") print(llist) llist.head = llist.reverse(3) print("Reversed Linked list") print(llist) # This code is contributed by Sagar Kumar Sinha(sagarsinha7777) Output: Given Linked List 1 2 3 4 5 6 7 8 9 Reversed list 3 2 1 6 5 4 9 8 7 Please refer complete article on Reverse a Linked List in groups of given size | Set 2 for more details! Comment More infoAdvertise with us Next Article Python Program For Reversing A Linked List In Groups Of Given Size- Set 2 kartik Follow Improve Article Tags : Linked List Python Python Programs DSA Linked Lists Microsoft Adobe VMWare Snapdeal Paytm Accolite MakeMyTrip Python-DSA +9 More Practice Tags : AccoliteAdobeMakeMyTripMicrosoftPaytmSnapdealVMWareLinked Listpython +5 More Similar Reads Python Program For Reversing A Linked List In Groups Of Given Size - Set 1 Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Input: 1->2->3->4->5->6->7->8->NULL, K = 3 Output: 3->2->1->6->5->4->8->7->NULL Input: 1->2->3->4->5->6->7->8->NULL, K = 5 Output: 5->4->3->2->1->8->7->6->NULL Recommended: Please solve 3 min read Python Program For Rearranging A Given Linked List In-Place Given a singly linked list L0 -> L1 -> ⦠-> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is: L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ...You are required to do this in place without altering the nodes' values. Examples: Input: 1 -> 2 -> 3 -> 6 min read Python Program For Reversing Alternate K Nodes In A Singly Linked List Given a linked list, write a function to reverse every alternate k nodes (where k is an input to the function) in an efficient way. Give the complexity of your algorithm. Example: Inputs: 1->2->3->4->5->6->7->8->9->NULL and k = 3 Output: 3->2->1->4->5->6- 6 min read Python Program For Alternating Split Of A Given Singly Linked List- Set 1 Write a function AlternatingSplit() that takes one list and divides up its nodes to make two smaller lists 'a' and 'b'. The sublists should be made from alternating elements in the original list. So if the original list is 0->1->0->1->0->1 then one sublist should be 0->0->0 and 3 min read Python Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes 4 min read Python Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2->4->6->8, Ou 4 min read Python Program For Pairwise Swapping Elements Of A Given Linked List Given a singly linked list, write a function to swap elements pairwise. Input: 1->2->3->4->5->6->NULL Output: 2->1->4->3->6->5->NULL Input: 1->2->3->4->5->NULL Output: 2->1->4->3->5->NULL Input: 1->NULL Output: 1->NULL For examp 2 min read Python Program For Swapping Nodes In A Linked List Without Swapping Data Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15- 5 min read Python Program For Rotating A Linked List 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 5 min read Python Program For Inserting Node In The Middle Of The Linked List Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node. Examples: Input : list: 1->2->4->5 x = 3 Output : 1->2-> 4 min read Like