Skip to content

Commit 16c579f

Browse files
authored
Merge pull request #2139 from solairerove/main
Create: 0147-insertion-sort-list.py
2 parents e2667b0 + 55605d1 commit 16c579f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: python/0147-insertion-sort-list.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
3+
if not head or not head.next:
4+
return head
5+
6+
sentinel = ListNode()
7+
curr = head
8+
while curr:
9+
prev = sentinel
10+
while prev.next and curr.val >= prev.next.val:
11+
prev = prev.next
12+
13+
curr.next, prev.next, curr = prev.next, curr, curr.next
14+
15+
return sentinel.next

0 commit comments

Comments
 (0)