Python Program For Pairwise Swapping Elements Of A Given Linked List By Changing Links
Last Updated :
12 Sep, 2022
Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1->2->3->4->5->6->7 then the function should change it to 2->1->4->3->6->5->7, and if the linked list is 1->2->3->4->5->6 then the function should change it to 2->1->4->3->6->5
This problem has been discussed here. The solution provided there swaps data of nodes. If data contains many fields, there will be many swap operations. So changing links is a better idea in general. Following is the implementation that changes links instead of swapping data.
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def addToList( self , data):
newNode = Node(data)
if self .head is None :
self .head = newNode
return
last = self .head
while last. next :
last = last. next
last. next = newNode
def __str__( self ):
linkedListStr = ""
temp = self .head
while temp:
linkedListStr = (linkedListStr +
str (temp.data) + " " )
temp = temp. next
return linkedListStr
def pairWiseSwap( self ):
if ( self .head is None or
self .head. next is None ):
return
prevNode = self .head
currNode = self .head. next
self .head = currNode
while True :
nextNode = currNode. next
currNode. next = prevNode
if nextNode. next is None :
prevNode. next = nextNode
break
prevNode. next = nextNode. next
prevNode = nextNode
currNode = prevNode. next
linkedList = LinkedList()
linkedList.addToList( 1 )
linkedList.addToList( 2 )
linkedList.addToList( 3 )
linkedList.addToList( 4 )
linkedList.addToList( 5 )
linkedList.addToList( 6 )
linkedList.addToList( 7 )
print ( "Linked list before calling"
"pairwiseSwap() " , linkedList)
linkedList.pairWiseSwap()
print ( "Linked list after calling "
"pairwiseSwap() " , linkedList)
|
Output:
Linked list before calling pairWiseSwap() 1 2 3 4 5 6 7
Linked list after calling pairWiseSwap() 2 1 4 3 6 5 7
Time Complexity: The time complexity of the above program is O(n) where n is the number of nodes in a given linked list. The while loop does a traversal of the given linked list.
Auxiliary Space: O(1) because constant space has been used
Following is the recursive implementation of the same approach. We change the first two nodes and recur for the remaining list. Thanks to geek and omer salem for suggesting this method.
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def addToList( self , data):
newNode = Node(data)
if self .head is None :
self .head = newNode
return
last = self .head
while last. next :
last = last. next
last. next = newNode
def __str__( self ):
linkedListStr = ""
temp = self .head
while temp:
linkedListStr = (linkedListStr +
str (temp.data) + " " )
temp = temp. next
return linkedListStr
def pairWiseSwap( self , node):
if node is None or node. next is None :
return node
remaining = node. next . next
newHead = node. next
node. next . next = node
node. next = self .pairWiseSwap(remaining)
return newHead
linkedList = LinkedList()
linkedList.addToList( 1 )
linkedList.addToList( 2 )
linkedList.addToList( 3 )
linkedList.addToList( 4 )
linkedList.addToList( 5 )
linkedList.addToList( 6 )
linkedList.addToList( 7 )
print ( "Linked list before calling "
"pairwiseSwap() " , linkedList)
linkedList.head = linkedList.pairWiseSwap(
linkedList.head)
print ( "Linked list after calling "
"pairwiseSwap() " , linkedList)
|
Output:
Linked list before calling pairWiseSwap() 1 2 3 4 5 6 7
Linked list after calling pairWiseSwap() 2 1 4 3 6 5 7
Please refer complete article on Pairwise swap elements of a given linked list by changing links for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...