Python Program For Alternating Split Of A Given Singly Linked List- Set 1 Last Updated : 23 Jan, 2023 Comments Improve Suggest changes Like Article Like Report 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 the other should be 1->1->1. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Method (Simple): The simplest approach iterates over the source list and pull nodes off the source and alternately put them at the front (or beginning) of 'a' and b'. The only strange part is that the nodes will be in the reverse order that occurred in the source list. Method 2 inserts the node at the end by keeping track of the last node in sublists. Python # Python program to alternatively split # a linked list into two halves # Node class class Node: def __init__(self, data, next = None): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None # Given the source list, split its nodes # into two shorter lists. If we number the # elements 0, 1, 2, ... then all the even # elements should go in the first list, and # all the odd elements in the second. The # elements in the new lists may be in any order. def AlternatingSplit(self, a, b): first = self.head second = first.next while (first is not None and second is not None and first.next is not None): # Move a node to list 'a' self.MoveNode(a, first) # Move a node to list 'b' self.MoveNode(b, second) first = first.next.next if first is None: break second = first.next # Pull off the front node of the # source and put it in dest def MoveNode(self, dest, node): # Make the new node new_node = Node(node.data) if dest.head is None: dest.head = new_node else: # Link the old dest off the # new node new_node.next = dest.head # Move dest to point to the # new node dest.head = new_node # Utility Functions # Function to insert a node at # the beginning of the linked list def push(self, data): # 1 & 2 allocate the Node & # put the data new_node = Node(data) # Make the next of new Node as head new_node.next = self.head # Move the head to point to # new Node self.head = new_node # Function to print nodes in a given # linked list def printList(self): temp = self.head while temp: print temp.data, temp = temp.next print("") # Driver Code if __name__ == "__main__": # Start with empty list llist = LinkedList() a = LinkedList() b = LinkedList() # Created linked list will be # 0->1->2->3->4->5 llist.push(5) llist.push(4) llist.push(3) llist.push(2) llist.push(1) llist.push(0) llist.AlternatingSplit(a, b) print "Original Linked List: ", llist.printList() print "Resultant Linked List 'a' : ", a.printList() print "Resultant Linked List 'b' : ", b.printList() # This code is contributed by kevalshah5 Output: Original linked List: 0 1 2 3 4 5 Resultant Linked List 'a' : 4 2 0 Resultant Linked List 'b' : 5 3 1 Time Complexity: O(n) where n is a number of nodes in the given linked list. Space Complexity: O(1),The time complexity for this algorithm is O(n) as we are traversing through the linked list only once.Please refer complete article on Alternating split of a given Singly Linked List | Set 1 for more details! Comment More infoAdvertise with us Next Article Python Program For Alternating Split Of A Given Singly Linked List- Set 1 kartik Follow Improve Article Tags : Linked List Python Python Programs DSA Linked Lists +1 More Practice Tags : Linked Listpython Similar Reads 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 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 Finding The Middle Element Of A Given Linked List Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. For example, if given linked list 4 min read Python Program for Deleting a Node in a Linked List We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from 3 min read 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 Reversing A Linked List In Groups Of Given Size- Set 2 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 3 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 Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1 3 min read Python Program For Sorting A Linked List Of 0s, 1s And 2s By Changing Links Given a linked list of 0s, 1s and 2s, sort it.Examples: Input: 2->1->2->1->1->2->0->1->0 Output: 0->0->1->1->1->1->2->2->2 The sorted Array is 0, 0, 1, 1, 1, 1, 2, 2, 2. Input: 2->1->0 Output: 0->1->2 The sorted Array is 0, 1, 2Recommended: 3 min read Python Program For Deleting A Node In A Doubly Linked List Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list 4 min read Like