LeetCode Partition List 非常简单易懂的解法
来源:互联网 发布:betterzip 4 for mac 编辑:程序博客网 时间:2024/06/05 07:30
/************************************************************************
* 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.
************************************************************************/
核心思想,用两个伪结点分别指向小于x值和大于等于x值,而且此方法不改变顺序。
ListNode *partition(ListNode *head, int x) { ListNode node1(0), node2(0); ListNode *p1 = &node1, *p2 = &node2; while (head) { if (head->val < x) p1 = p1->next = head; //分解成两句 p1->next=head; else //p1=p1->next; p2 = p2->next = head; //同上 head = head->next; } p2->next = NULL; p1->next = node2.next; return node1.next;}
0 0
- LeetCode Partition List 非常简单易懂的解法
- LeetCode Rotate List简单易懂解法
- LeetCode Valid Parentheses非常简单的解法
- LeetCode 8. String to Integer (atoi)简单易懂的解法
- Restful Webservice非常简单易懂的实例
- Partition List问题及解法
- 【leetcode】109. Convert Sorted List to Binary Search Tree【java】 简单易懂的方法
- leetcode--561--Array Partition I(我的解法)
- LeetCode: Partition List
- LeetCode Partition List
- LeetCode: Partition List
- [Leetcode] Partition List **
- [Leetcode] Partition List
- [LeetCode] Partition List
- leetcode 81: Partition List
- [Leetcode] Partition List
- leetcode 118: Partition List
- [LeetCode]Partition List
- 从宜人贷系统架构看互联网高并发对金融系统架构的挑战
- Redis的安装及单机Redis测试
- 设计模式之原型模式(C++)
- DBCP连接池配置参数说明
- 使用命令生成puppet.conf无法启动puppet master问题解决
- LeetCode Partition List 非常简单易懂的解法
- 【NOIP2016模拟7.11】树 [bzoj] 4551: [Tjoi2016&Heoi2016]树
- 查看Struts2配置信息 struts2-config-browser-plugin插件
- SciPy Hierarchical Clustering and Dendrogram Tutorial
- isConnected failed: EHOSTUNREACH网络不通有可能是路由限制了
- Android中socket的应用
- js 函数定义三种方式
- MySQL:event的简单使用
- Java并发编程:Callable、Future和FutureTask