Rearrange a linked list in such a way that all odd position nodes are together and all even positions node are together,
Examples:
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 the below cases are handled
- Empty linked list.
- A linked list with only one node.
- A linked list with only two nodes.
- A linked list with an odd number of nodes.
- 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
class Node:
def __init__( self , d):
self .data = d
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def newNode( self , key):
temp = Node(key)
self . next = None
return temp
def rearrangeEvenOdd( self , head):
if ( self .head = = None ):
return None
odd = self .head
even = self .head. next
evenFirst = even
while ( 1 = = 1 ):
if (odd = = None or even = = None or
(even. next ) = = None ):
odd. next = evenFirst
break
odd. next = even. next
odd = even. next
if (odd. next = = None ):
even. next = None
odd. next = evenFirst
break
even. next = odd. next
even = odd. next
return head
def printlist( self , node):
while (node ! = None ):
print (node.data, end = "")
print ( "->" , end = "")
node = node. next
print ( "NULL" )
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
ll = LinkedList()
ll.push( 5 )
ll.push( 4 )
ll.push( 3 )
ll.push( 2 )
ll.push( 1 )
print ( "Given Linked List" )
ll.printlist(ll.head)
start = ll.rearrangeEvenOdd(ll.head)
print ( "Modified Linked List" )
ll.printlist(start)
|
OutputGiven Linked List
1->2->3->4->5->NULL
Modified Linked List
1->3->5->2->4->NULL
Time Complexity: O(n) where n is the number of nodes in the given linked list.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Approach: Two-Pointer Approach
The steps for the Two-Pointer Approach for rearranging a linked list such that all odd-positioned nodes are together and all even-positioned nodes are together are as follows:
- Create two new nodes to represent the heads of the odd and even positioned nodes, respectively.
- Create two pointers, odd_ptr and even_ptr, which initially point to these head nodes.
- Traverse the original linked list using a while loop, and for each node, append it to the end of either the odd list or the even list depending on its position.
- To append a node to the end of a list, set the next pointer of the last node in the list to the new node, and then update the next pointer of the new node to None.
- To keep track of whether the current node is odd or even, use a boolean variable is_odd and flip its value at each iteration using is_odd = not is_odd.
- After traversing the original linked list, append the even list to the end of the odd list by setting the next pointer of the last node in the odd list to the head of the even list.
- Set the next pointer of the last node in the even list to None to terminate the new linked list.
- Return the head of the rearranged linked list by skipping over the dummy head nodes that were created at the beginning.
Python3
class Node:
def __init__( self , val = 0 , next = None ):
self .val = val
self . next = next
def rearrangeLinkedList(head: Node) - > Node:
if not head or not head. next :
return head
odd_ptr = odd_head = Node( 0 )
even_ptr = even_head = Node( 0 )
is_odd = True
while head:
if is_odd:
odd_ptr. next = head
odd_ptr = odd_ptr. next
else :
even_ptr. next = head
even_ptr = even_ptr. next
is_odd = not is_odd
head = head. next
odd_ptr. next = even_head. next
even_ptr. next = None
return odd_head. next
head = Node( 1 )
head. next = Node( 2 )
head. next . next = Node( 3 )
head. next . next . next = Node( 4 )
new_head = rearrangeLinkedList(head)
while new_head:
print (new_head.val, end = "->" )
new_head = new_head. next
|
The time complexity: O(N),
The auxiliary space: O(1)
Method: Split and Merge
Steps:
- Create two new linked lists, one for odd positioned nodes and the other for even positioned nodes.
- Traverse the original linked list and insert the nodes at odd positions into the odd list and nodes at even positions into the even list.
- Combine the odd list and even list to get the desired rearranged linked list.
Python3
class Node:
def __init__( self , data = None ):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def printList( self ):
temp = self .head
while (temp):
print (temp.data, end = " " )
temp = temp. next
def rearrange( self ):
if self .head is None :
return
odd = Node( self .head.data)
even = Node( self .head. next .data)
odd_tail = odd
even_tail = even
curr_node = self .head. next . next
is_odd = True
while curr_node is not None :
if is_odd:
odd_tail. next = Node(curr_node.data)
odd_tail = odd_tail. next
else :
even_tail. next = Node(curr_node.data)
even_tail = even_tail. next
curr_node = curr_node. next
is_odd = not is_odd
odd_tail. next = even
self .head = odd
llist = LinkedList()
llist.push( 4 )
llist.push( 3 )
llist.push( 2 )
llist.push( 1 )
print ( "Original linked list: " )
llist.printList()
llist.rearrange()
print ( "\nRearranged linked list: " )
llist.printList()
llist = LinkedList()
llist.push( 70 )
llist.push( 56 )
llist.push( 43 )
llist.push( 30 )
llist.push( 22 )
llist.push( 10 )
print ( "\nOriginal linked list: " )
llist.printList()
llist.rearrange()
print ( "\nRearranged linked list: " )
llist.printList()
|
OutputOriginal linked list:
1 2 3 4
Rearranged linked list:
1 3 2 4
Original linked list:
10 22 30 43 56 70
Rearranged linked list:
10 30 56 22 43 70
Time complexity: O(n), where n is the number of nodes in the linked list.
Auxiliary space: O(n), where n is the number of nodes in the linked list, as we are creating two separate linked lists for odd and even positioned nodes.
Please refer complete article on Rearrange a linked list such that all even and odd positioned nodes are together for more details!