Sort Linked List Already Sorted Using Absolute Values

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
    def sortLinkedList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        pp = None
        p = head
        while p is not None:
            if p.val < 0 and pp is not None:
                pp.next = p.next
                p.next = head
                head = p
                p = pp
            pp = p
            p = p.next
        return head