Skip to content
Related Articles

Related Articles

Python Program For Alternating Split Of A Given Singly Linked List- Set 1

Improve Article
Save Article
Like Article
  • Last Updated : 16 Dec, 2021

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.

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 program to alternatively split 
# a linked list into two halves 
# Node class
class Node:    
    def __init__(self, data, 
                 next = None):        = data = 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 =
        while (first is not None and 
               second is not None and 
      is not None):
              # Move a node to list 'a'
              self.MoveNode(a, first) 
              # Move a node to list 'b'
              self.MoveNode(b, second) 
              first =
              if first is None:
              second =
    # 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(
        if dest.head is None:
            dest.head = new_node
            # Link the old dest off the 
            # new node 
   = 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 = 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:
            temp =
# 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.AlternatingSplit(a, b)
    print "Original Linked List: ",
    print "Resultant Linked List 'a' : ",
    print "Resultant Linked List 'b' : ",
# This code is contributed by kevalshah5


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.
Please refer complete article on Alternating split of a given Singly Linked List | Set 1 for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!