imashutosh51

Absolute linked list sorting

Aug 10th, 2022 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. '''
  2. Logic:
  3. if number is positive,then no change
  4. else number will be in left side of head node and head node will be updated.
  5. '''
  6. class Node:
  7.  
  8.     def __init__(self, data):  # data -> value stored in node
  9.         self.data = data
  10.         self.next = None
  11.  
  12.  
  13. class Solution:
  14.     def sortList(self, head):
  15.         if not head:
  16.             return head
  17.  
  18.         prev, cur = head, head.next
  19.  
  20.         while cur:
  21.             if cur.data < 0:
  22.                 prev.next = cur.next
  23.                 cur.next = head
  24.                 head = cur
  25.                 cur = prev.next
  26.             else:
  27.                 prev, cur = cur, cur.next
  28.  
  29.         return head
  30.  
Advertisement
Add Comment
Please, Sign In to add comment