Advertisement
avisrivastava254084

Untitled

Oct 25th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. # Python3 program to rearrange a linked list
  2. # in such a way that all odd positioned
  3. # node are stored before all even positioned nodes
  4.  
  5. # Linked List Node
  6. class Node:
  7.     def __init__(self, d):
  8.         self.data = d
  9.         self.next = None
  10.  
  11. class LinkedList:
  12.     def __init__(self):
  13.         self.head = None
  14.        
  15.     # A utility function to create
  16.     # a new node
  17.     def newNode(self, key):
  18.         temp = Node(key)
  19.         self.next = None
  20.         return temp
  21.  
  22.     # Rearranges given linked list
  23.     # such that all even positioned
  24.     # nodes are before odd positioned.
  25.     # Returns new head of linked List.
  26.     def rearrangeEvenOdd(self, head):
  27.        
  28.         # Corner case
  29.         if (self.head == None):
  30.             return None
  31.  
  32.         # Initialize first nodes of
  33.         # even and odd lists
  34.         odd = self.head
  35.         even = self.head.next
  36.  
  37.         # Remember the first node of even list so
  38.         # that we can connect the even list at the
  39.         # end of odd list.
  40.         evenFirst = even
  41.  
  42.         while (1 == 1):
  43.            
  44.             # If there are no more nodes,
  45.             # then connect first node of even
  46.             # list to the last node of odd list
  47.             if (odd == None or even == None or
  48.                             (even.next) == None):
  49.                 odd.next = evenFirst
  50.                 break
  51.  
  52.             # Connecting odd nodes
  53.             odd.next = even.next
  54.             odd = even.next
  55.  
  56.             # If there are NO more even nodes
  57.             # after current odd.
  58.             if (odd.next == None):
  59.                 even.next = None
  60.                 odd.next = evenFirst
  61.                 break
  62.  
  63.             # Connecting even nodes
  64.             even.next = odd.next
  65.             even = odd.next
  66.         return head
  67.  
  68.     # A utility function to print a linked list
  69.     def printlist(self, node):
  70.         while (node != None):
  71.             print(node.data, end = "")
  72.             print("->", end = "")
  73.             node = node.next
  74.         print ("NULL")
  75.        
  76.     # Function to insert a new node
  77.     # at the beginning
  78.     def push(self, new_data):
  79.         new_node = Node(new_data)
  80.         new_node.next = self.head
  81.         self.head = new_node
  82.  
  83. # Driver code
  84. ll = LinkedList()
  85. ll.push(5)
  86. ll.push(4)
  87. ll.push(3)
  88. ll.push(2)
  89. ll.push(1)
  90. print ("Given Linked List")
  91. ll.printlist(ll.head)
  92.  
  93. start = ll.rearrangeEvenOdd(ll.head)
  94.  
  95. print ("\nModified Linked List")
  96. ll.printlist(start)
  97.  
  98. # This code is contributed by Prerna Saini
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement