Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Program For Rearranging A Linked List Such That All Even And Odd Positioned Nodes Are Together

  • Last Updated : 18 May, 2022

Rearrange a linked list in such a way that all odd position nodes are together and all even positions node are together, 

Input:   1->2->3->4
Output:  1->3->2->4

Input:   10->22->30->43->56->70
Output:  10->30->56->22->43->70

The important thing in this question is to make sure that all below cases are handled 

  1. Empty linked list.
  2. A linked list with only one node.
  3. A linked list with only two nodes.
  4. A linked list with an odd number of nodes.
  5. A linked list with an even number of nodes.

The below program maintains two pointers ‘odd’ and ‘even’ for current nodes at odd and even positions respectively. We also store the first node of the even linked list so that we can attach the even list at the end of the odd list after all odd and even nodes are connected together in two different lists.


# Python3 program to rearrange a 
# linked list in such a way that 
# all odd positioned node are stored 
# before all even positioned nodes 
# Linked List Node 
class Node: 
    def __init__(self, d): = d = None
class LinkedList:
    def __init__(self):
        self.head = None
    # A utility function to create
    # a new node 
    def newNode(self, key): 
        temp = Node(key) = None
        return temp 
    # Rearranges given linked list 
    # such that all even positioned 
    # nodes are before odd positioned. 
    # Returns new head of linked List. 
    def rearrangeEvenOdd(self, head): 
        # Corner case 
        if (self.head == None): 
            return None
        # Initialize first nodes of 
        # even and odd lists 
        odd = self.head 
        even =
        # Remember the first node of 
        # even list so that we can 
        # connect the even list at the 
        # end of odd list. 
        evenFirst = even 
        while (1 == 1): 
            # If there are no more nodes, 
            # then connect first node of even 
            # list to the last node of odd list 
            if (odd == None or even == None or 
               ( == None): 
       = evenFirst 
            # Connecting odd nodes 
            odd =
            # If there are NO more even nodes 
            # after current odd. 
            if ( == None): 
       = None
       = evenFirst 
            # Connecting even nodes 
            even =
        return head
    # A utility function to print a 
    # linked list 
    def printlist(self, node): 
        while (node != None): 
            print(, end = "")
            print("->", end = "")
            node =
        print ("NULL")
    # Function to insert a new node 
    # at the beginning 
    def push(self, new_data): 
        new_node = Node(new_data) = self.head 
        self.head = new_node
# Driver code 
ll = LinkedList()
print ("Given Linked List")
start = ll.rearrangeEvenOdd(ll.head) 
print ("Modified Linked List")
# This code is contributed by Prerna Saini


Given Linked List
Modified Linked List

Please refer complete article on Rearrange a linked list such that all even and odd positioned nodes are together for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!