-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion-sort-list.py
31 lines (28 loc) · 1.05 KB
/
insertion-sort-list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
# Create a dummy node before the head
dummy = ListNode(0)
dummy.next = head
# Traverse the list from the second node to the end
curr = head
while curr and curr.next:
# If the next node is smaller than the current node,
# remove the next node from the list and find its insertion point
if curr.next.val < curr.val:
prev = dummy
while prev.next.val < curr.next.val:
prev = prev.next
# Insert the next node at its insertion point
tmp = curr.next
curr.next = tmp.next
tmp.next = prev.next
prev.next = tmp
else:
# Move to the next node
curr = curr.next
# Return the sorted list
return dummy.next