Skip to content

Commit

Permalink
0147 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Feb 13, 2019
1 parent 5d56359 commit 898e299
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 0147-Insertion-Sort-List/cpp-0147/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.13)
project(cpp_0147)

set(CMAKE_CXX_STANDARD 11)

add_executable(cpp_0147 main.cpp)
111 changes: 111 additions & 0 deletions 0147-Insertion-Sort-List/cpp-0147/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/// Source : https://leetcode.com/problems/insertion-sort-list/
/// Author : liuyubobobo
/// Time : 2019-02-13

#include <iostream>

using namespace std;


/// Ad-Hoc
/// Time Complexity: O(n^2)
/// Space Compelxity: O(1)

/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};


ListNode* createLinkedList(int arr[], int n){

if(n == 0)
return NULL;

ListNode* head = new ListNode(arr[0]);
ListNode* curNode = head;
for(int i = 1 ; i < n ; i ++){
curNode->next = new ListNode(arr[i]);
curNode = curNode->next;
}

return head;
}

void printLinkedList(ListNode* head){

ListNode* curNode = head;
while(curNode != NULL){
cout << curNode->val << " -> ";
curNode = curNode->next;
}

cout << "NULL" << endl;

return;
}

void deleteLinkedList(ListNode* head){

ListNode* curNode = head;
while(curNode != NULL){
ListNode* delNode = curNode;
curNode = curNode->next;
delete delNode;
}

return;
}

class Solution {
public:
ListNode* insertionSortList(ListNode* head) {

if(!head || !head->next) return head;

ListNode* dummyHead = new ListNode(-1);
dummyHead->next = head;

ListNode* pre = dummyHead->next;
while(pre->next){
int val = pre->next->val;
ListNode* next = pre->next->next;
ListNode* pi = dummyHead;
for(; pi != pre; pi = pi->next)
if(pi->next->val > val){
ListNode* pj = pi->next;
ListNode* swapNode = pre->next;

pi->next = swapNode;
swapNode->next = pj;
pre->next = next;

break;
}

if(pi == pre) pre = pre->next;
// printLinkedList(dummyHead);
}
return dummyHead->next;
}
};


int main() {

int arr1[4] = {4, 2, 1, 3};
ListNode* head1 = createLinkedList(arr1, 4);
ListNode* res1 = Solution().insertionSortList(head1);
printLinkedList(res1);
deleteLinkedList(res1);

int arr2[5] = {-1, 5, 3, 4, 0};
ListNode* head2 = createLinkedList(arr2, 5);
ListNode* res2 = Solution().insertionSortList(head2);
printLinkedList(res2);
deleteLinkedList(res2);

return 0;
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ email: [[email protected]](mailto:[email protected])
| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/description/) | [solution](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [C++](0144-Binary-Tree-Preorder-Traversal/cpp-0144/) | [Java](0144-Binary-Tree-Preorder-Traversal/java-0144/src/) | |
| 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/description/) | [solution](https://leetcode.com/problems/binary-tree-postorder-traversal/solution/) | [C++](0145-Binary-Tree-Postorder-Traversal/cpp-0145/) | [Java](0145-Binary-Tree-Postorder-Traversal/java-0145/src/) | |
| | | | | | |
| 147 | [Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/) | [C++](0147-Insertion-Sort-List/cpp-0147/) | | | |
| 148 | [Sort List](https://leetcode.com/problems/sort-list/description/) | [] | [C++](0148-Sort-List/cpp-0148/) | | |
| 149 | [Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/description/) | [solution](https://leetcode.com/problems/max-points-on-a-line/solution/) | [C++](0149-Max-Points-on-a-Line/cpp-0149/) | | |
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/description/) | [] | [C++](0150-Evaluate-Reverse-Polish-Notation/cpp-0150/) | | |
Expand Down

0 comments on commit 898e299

Please sign in to comment.