[Leetcode] #86 Partition List
来源:互联网 发布:java软件工程师辛苦么 编辑:程序博客网 时间:2024/06/06 13:01
Discription:
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
.
Solution:
ListNode* partition(ListNode* head, int x) {if (!head) return NULL;ListNode *head1 = new ListNode(-1);head1->next = head;head = head1;ListNode *head2 = new ListNode(-1); ListNode *cur = head2;while (head1->next){if (head1->next->val >= x){cur->next = head1->next;cur = cur->next;head1->next = head1->next->next;}elsehead1 = head1->next;}cur->next = NULL;head1->next = head2->next;return head->next;}
ListNode *partition(ListNode *head, int x) {ListNode node1(0), node2(0);ListNode *p1 = &node1, *p2 = &node2;while (head) {if (head->val < x){p1->next = head;p1 = p1->next;}else{p2->next = head;p2 = p2->next;}head = head->next;}p2->next = NULL;p1->next = node2.next;return node1.next;}
0 0
- LeetCode 86: Partition List
- LeetCode 86 Partition List
- [leetcode 86] Partition List
- leetcode-86 Partition List
- [LeetCode 86]Partition List
- leetcode || 86、Partition List
- LeetCode---(86) Partition List
- LeetCode 86 Partition List
- Leetcode[86]-Partition List
- leetcode[86]:Partition List
- Leetcode #86 Partition List
- LeetCode(86) Partition List
- leetcode 86: Partition List
- [Leetcode]#86 Partition List
- leetCode #86 Partition List
- leetcode 86:Partition List
- 【leetcode】【86】Partition List
- leetcode-Partition List-86
- Maven 手动安装jar到本地仓库
- 今日头条2017后端工程师实习生笔试题——最大映射
- 50 道 Java 基础编程练习题
- ssh端口转发开启mysql“外链”
- 转义字符表
- [Leetcode] #86 Partition List
- Tensorflow平台搭建和使用:计划及安排
- 关于框架的数据表模型
- 手写代码实现EventBus
- java三大框架
- 在CentOS 7下安装Nginx
- SpringMVC数据校验
- Dialog详解(包括进度条、PopupWindow、自定义view、自定义样式的对话框)
- C# TableLayoutPanel 添加、删除一行数据案例