Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created April 4, 2017 14:01
Show Gist options
  • Save s4553711/4a178cc1d2d39bf44d9e84e4cb916e69 to your computer and use it in GitHub Desktop.
Save s4553711/4a178cc1d2d39bf44d9e84e4cb916e69 to your computer and use it in GitHub Desktop.
/**
* 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