Skip to content

Commit 4e1fc06

Browse files
🔢 Day 2
1 parent ef96f89 commit 4e1fc06

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
## Week 1 🚧
88
1. [Convert Binary Number in a Linked List to Integer](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3516/) ➡️ [CPP Solution](Week1/getDecimalValue.cpp)
9+
2. [Insertion Sort List](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3517/) ➡️ [CPP Solution](Week1/insertionSortList.cpp)
910

1011
## Week 2 🚧
1112
Coming soon...

Week1/insertionSortList.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* insertionSortList(ListNode* head) {
14+
ListNode* dummy = new ListNode(-1);
15+
ListNode* curr = head;
16+
17+
while(curr != NULL) {
18+
19+
ListNode* temp = curr->next;
20+
ListNode* prev = dummy;
21+
ListNode* next = dummy->next;
22+
23+
while(next != NULL && next->val < curr->val) {
24+
prev = next;
25+
next = next->next;
26+
}
27+
28+
curr->next = next;
29+
prev->next = curr;
30+
curr = temp;
31+
32+
}
33+
34+
return dummy->next;
35+
}
36+
};

0 commit comments

Comments
 (0)