-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path147.cpp
30 lines (30 loc) · 852 Bytes
/
147.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 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) {
if(head == NULL) return NULL;
ListNode* pre = new ListNode(0);
ListNode* worker = head, *ender = head, *nower = head;
while(ender != NULL)
{
ender = ender->next;
nower = pre;
// 寻找位置
while(nower -> next != NULL && nower->next->val < worker->val)
nower = nower->next;
// 插入节点
worker->next = nower->next;
nower->next = worker;
worker = ender;
}
return pre->next;
}
};