LeetCode 086 Partition List
来源:互联网 发布:sqlserver 创建临时表 编辑:程序博客网 时间:2024/06/17 17:49
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 遍历完成后,注意对两个链表的连接和结尾处理。
public ListNode partition(ListNode head, int x) { if(head == null){ return null; } ListNode dummy1 = new ListNode(Integer.MIN_VALUE); ListNode dummy2 = new ListNode(Integer.MAX_VALUE); ListNode cur1 = dummy1; ListNode cur2 = dummy2; ListNode cur = head; while(cur!=null){ if(cur.val>=x){ cur2.next = cur; cur2 = cur2.next; } else{ cur1.next = cur; cur1 = cur1.next; } cur = cur.next; } cur1.next = dummy2.next; cur2.next = null; return dummy1.next; }
0 0
- LeetCode: Partition List [086]
- LeetCode 086 Partition List
- 086 Partition List [Leetcode]
- leetcode 086 Partition List
- leetcode 086 —— Partition List
- 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
- [leetcode]Partition List
- LeetCode-Partition List
- [leetcode] Partition List
- Windows Azure Mobile Service - Data Sync lessons learned
- Lua的function、closure和upvalue
- Hello my blog on CSDN
- KVC 和KVO机制
- 使用C++ ostringstream来格式化字符串输出
- LeetCode 086 Partition List
- Codeforces 347 B Fixed Points 题解
- 关于Fragment的学习笔记1——利用布局文件创建固定Fragment
- Web开发对*.jsp的过滤
- 遗传算法的手工模拟计算示例
- Cocos2d-x-3.2学习笔记
- drm:Direct Rendering Manager
- 存储班长信息的学生类
- Android应用自动更新功能的实现!!!软件更新,自动下载,安装