Python Program For Alternating Split Of A Given Singly Linked List- Set 1 Last Updated : 23 Jan, 2023 Summarize Comments Improve Suggest changes Share 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 Random Singly Linked List Generator using Python K kartik Follow Improve Article Tags : Python Linked Lists Practice Tags : python Similar Reads Javascript 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 Random Singly Linked List Generator using Python In this article, we will learn how to generate a singly linked list using random numbers. Prerequisite: Insertion in Linked List A Singly Linked List is a linear data structure. A linked list is a collection of nodes and in a singly linked list every node contains two things: Data Reference to the n 5 min read Implementation of XOR Linked List in Python Prerequisite: XOR Linked List An ordinary Doubly Linked List requires space for two address fields to store the addresses of previous and next nodes. A memory-efficient version of Doubly Linked List can be created using only one space for the address field with every node. This memory efficient Doub 6 min read Singly Linked List in Python A Singly Linked List is a type of data structure that is made up of nodes that are created using self-referential structures. Each node contains a data element and a reference (link) to the next node in the sequence. This allows for a dynamic and efficient management of data elements. Table of Conte 10 min read Partition a Linked List into K continuous groups with difference in their sizes at most 1 Given a linked list consisting of n nodes and an integer k, the task is to split the given Linked List into k continuous groups such that the difference between the size of the adjacent groups after splitting is at most 1 and the groups are sorted in descending order of their lengths. Note: A group 9 min read Python | Add element at alternate position in list Sometimes, while working with Python list, we can have a problem in which we need to add elements in list alternatively i.e at even positions and reorder the list accordingly. This has a potential application in many domains such as day-day programming and competitive programming. Let's discuss cert 2 min read Like