57_leetcode_partition List
来源:互联网 发布:数控车床g76螺纹编程 编辑:程序博客网 时间:2024/05/19 22:56
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given 1->4->3->2->5->2
and x = 3,
return 1->2->2->4->3->5
.
1:特殊情况;2:把链表分成两个子链表;3:最后考虑如果一个子链表为空的情况
ListNode *partition(ListNode *head,int x) { if(head == NULL || head->next == NULL) { return head; } ListNode* newHead = NULL; ListNode* index = NULL; ListNode* bigHead = NULL; ListNode* bigIndex = NULL; ListNode* curNode = head; while(curNode) { if(curNode->val >= x) { if(bigHead == NULL) { bigHead = curNode; bigIndex = bigHead; } else { bigIndex->next = curNode; bigIndex = bigIndex->next; } curNode = curNode->next; } else { if(newHead == NULL) { newHead = curNode; index = newHead; } else { index->next = curNode; index = index->next; } curNode = curNode->next; } } if(index) { index->next = bigHead; if(bigIndex) { bigIndex->next = NULL; } } else { newHead = bigHead; bigIndex->next = NULL; } return newHead; }
0 0
- 57_leetcode_partition List
- 57-Palindrome Linked List
- List
- list
- List
- LIST
- list
- list
- List
- list
- list
- List
- list
- LIST
- List
- List
- List
- list
- 56_leetcode_Remove Duplicates from Sorted Lists II
- 敬佛了附件礼物了报告分为拉克服了
- Autofac介绍
- 找到属于自己的路——leo鉴书62
- Oracle中as关键字作为别名使用时的用法详解
- 57_leetcode_partition List
- 使用Fiddler提高前端工作效率 (介绍篇)
- Oracle下row_number()over函数使用备忘
- 发表
- oracle分析函数row_number() over()使用
- 使用Fiddler提高前端工作效率 (实例篇)
- 58_leetcode_Sort List
- csdn肿么了,这两天写的博文都是待审核
- 宾馆的发挥地方和法国合肥和