Iam_Sandeep

Untitled

Sep 14th, 2022
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. class Node:
  2.     def __init__(self,data):
  3.         self.data=data
  4.         self.next=None
  5.    
  6. cur=root=Node(-1)
  7. #creeate linked list
  8. #[2,14,100]
  9. #[13,15,17,19]
  10. for i in [2,13,14,15,17,19,100]:
  11.     cur.next=Node(i)
  12.     cur=cur.next
  13. root=root.next
  14.  
  15. def display(x):
  16.     while x!=None:
  17.         print(x.data,end=" ")
  18.         x=x.next
  19.     print()
  20.  
  21. def separate(root):
  22.     odd_head  = odd  = Node(-1)
  23.     even_head = even = Node(-1)
  24.    
  25.     cur=root
  26.     while cur!=None:
  27.         if cur.data%2==0:
  28.             even.next=cur
  29.             even=even.next
  30.         else:
  31.             odd.next=cur
  32.             odd=odd.next
  33.         cur=cur.next
  34.    
  35.    
  36.     odd.next=None
  37.     even.next=None
  38.     return odd_head.next,even_head.next
  39.  
  40. display(root)
  41. odd_head,even_head=separate(root)
  42. display(odd_head)
  43. display(even_head)
  44.  
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment