147. Insertion Sort List
Medium
Last updated
Input: head = [4,2,1,3]
Output: [1,2,3,4]# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
cur = parent = ListNode(0)
while head:
while cur.next and cur.next.val < head.val:
cur = cur.next
cur.next, head.next, head = head, cur.next, head.next
# Optimization
if head and cur.val > head.val:
cur = parent
return parent.next