diff --git a/147.Insertion Sort List.cpp b/147.Insertion Sort List.cpp index cd185e4..1b3c94d 100644 --- a/147.Insertion Sort List.cpp +++ b/147.Insertion Sort List.cpp @@ -19,3 +19,53 @@ class Solution { return dummy->next; } }; + +https://www.programiz.com/cpp-programming/online-compiler/ +// Online C++ compiler to run C++ program online +#include + +struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} + }; + + +class Solution { +public: + ListNode* insertionSortList(ListNode* head) { + if(head == NULL || head->next == NULL ) return head; + ListNode *dummy = new ListNode(0), *cur = head; + // dummy->next=head; + while(cur){ + std::cout << "out:: " << cur->val<< std::endl; + ListNode *prev = dummy, *next = cur->next; + while(prev->next && prev->next->val<=cur->val){ + std::cout << "in:: " << prev->next->val<< std::endl; + // 0,4,2,5 + prev = prev->next; + } + cur->next = prev->next; + prev->next=cur; + std::cout << "last:: " << prev->next->val<< std::endl; + cur = next; + } + return dummy->next; + } +}; + + +int main() { + // Write C++ code here + // std::cout << "Try programiz.pro"; + Solution s= Solution(); + // [4,2,1,3] + ListNode *a = new ListNode(4); + ListNode *b = new ListNode(2); + a->next = b; + ListNode *c =s.insertionSortList(a); + std::cout << "res: "<val; + return 0; +}