LeetCode 86. Partition List
来源:互联网 发布:r语言编程艺术 pdf 编辑:程序博客网 时间:2024/04/27 02:22
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
.
Subscribe to see which companies asked this question
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* partition(ListNode *head, int x) { ListNode smaller_head(0); ListNode larger_head(0); ListNode *smaller_tail = &smaller_head; ListNode *larger_tail = &larger_head; while (head != NULL) { if (head->val < x) { smaller_tail->next = head; smaller_tail = head; } else { larger_tail->next = head; larger_tail = head; } head = head->next; } smaller_tail->next = larger_head.next; larger_tail->next = NULL; return smaller_head.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
- 86. Partition List LeetCode
- 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
- 博为峰Java技术文章 ——JavaEE Hibernate保存数据
- 中文分割
- 开博啦
- Unity 5 中的全局光照技术详解
- shell中的printf命令
- LeetCode 86. Partition List
- make命令行选项
- PHP: php_ldap.dll不能加载解决方案
- R之数据集
- jQuery——内容与表单选择器
- java sha1 加密
- LeetCode 83. Remove Duplicates from Sorted List
- Android Activity 生命周期
- numpy.random.shuffle打乱顺序函数