59_leetcode_Insertion Sort List
2014-06-20 07:24
260 查看
Sort a linked list using insertion sort.
1:插入排序;2:将链表中的每一个节点插入到已经排号的序列中
ListNode *insertionSortList(ListNode *head)
{
if(head == NULL || head->next == NULL)
{
return head;
}
ListNode* newHead = head;
ListNode* index = newHead;
ListNode* nextNode = head->next;
newHead->next = NULL;
ListNode* preNode = NULL;
ListNode* curNode = newHead;
while(nextNode)
{
ListNode* tempNode = nextNode->next;
while(curNode && curNode->val < nextNode->val)
{
preNode = curNode;
curNode = curNode->next;
}
if(curNode == NULL)
{
index->next = nextNode;
index = index->next;
index->next = NULL;
}
else if(curNode == newHead)
{
nextNode->next = newHead;
newHead = nextNode;
}
else
{
nextNode->next = curNode;
preNode->next = nextNode;
}
preNode = NULL;
curNode = newHead;
nextNode = tempNode;
}
return newHead;
}
1:插入排序;2:将链表中的每一个节点插入到已经排号的序列中
ListNode *insertionSortList(ListNode *head)
{
if(head == NULL || head->next == NULL)
{
return head;
}
ListNode* newHead = head;
ListNode* index = newHead;
ListNode* nextNode = head->next;
newHead->next = NULL;
ListNode* preNode = NULL;
ListNode* curNode = newHead;
while(nextNode)
{
ListNode* tempNode = nextNode->next;
while(curNode && curNode->val < nextNode->val)
{
preNode = curNode;
curNode = curNode->next;
}
if(curNode == NULL)
{
index->next = nextNode;
index = index->next;
index->next = NULL;
}
else if(curNode == newHead)
{
nextNode->next = newHead;
newHead = nextNode;
}
else
{
nextNode->next = curNode;
preNode->next = nextNode;
}
preNode = NULL;
curNode = newHead;
nextNode = tempNode;
}
return newHead;
}
相关文章推荐
- leetcode Insertion Sort List
- leetcode Insertion Sort List
- Insertion Sort List Leetcode java
- leetcode-Insertion Sort List
- leetcode-Insertion Sort List
- leetCode(4):Insertion Sort List
- 【leetcode】Insertion Sort List
- leetcode---insertion-sort-list---链表
- Leetcode:insertion-sort-list
- Insertion Sort List (LeetCode)
- leetcode[147]:Insertion Sort List
- 【LeetCode】Insertion Sort List
- Leetcode-insertion-sort-list ***
- LeetCode: Insertion Sort List
- 【leetcode】【单链表】【147】Insertion Sort List
- leetcode 147 Insertion Sort List java 算法
- LeetCode:Insertion Sort List
- Leetcode NO.147 Insertion Sort List
- [LeetCode] Insertion Sort List
- LeetCode--insertion-sort-list