Skip to main content

2046. Sort Linked List Already Sorted Using Absolute Values

link

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def sortLinkedList(self, head: Optional[ListNode]) -> Optional[ListNode]:

start, end = head, head

node = head

while node.next:
if node.next.val > 0:
end = node.next
node = node.next
else:
neg = node.next
end.next = neg.next
neg.next = start
start = neg

node = end

return start