Created
April 4, 2017 14:01
-
-
Save s4553711/4a178cc1d2d39bf44d9e84e4cb916e69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
ListNode* insertionSortList(ListNode* head) { | |
ListNode* dummy = new ListNode(0); | |
while(head != NULL) { | |
ListNode* node = dummy; | |
while(node->next != NULL && node->next->val < head->val) { | |
node = node->next; | |
} | |
ListNode* tmp = head->next; | |
head->next = node->next; | |
node->next = head; | |
head = tmp; | |
} | |
return dummy->next; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment