Leetcode - Partition List.
来源:互联网 发布:win10没有解压软件 编辑:程序博客网 时间:2024/05/22 15:39
https://oj.leetcode.com/problems/partition-list/
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.
public ListNode partition(ListNode head, int x)
这一题的解题思路很简单,就是根据x值构造两个list,一个list小于x,另一个list大于等于x即可,用一个tmp指针一路往下扫一路构建。然后连在一起就可以了。需要注意的不过是一些边界条件,譬如说x大于或者小于链表上的所有数。
public ListNode partition(ListNode head, int x) { ListNode smallHead = null, smalltmp = null, largeHead = null, largetmp = null, tmp = head; while(tmp != null){ if(tmp.val < x){ if(smallHead == null){ smallHead = tmp; smalltmp = smallHead; }else{ smalltmp.next = tmp; smalltmp = smalltmp.next; } }else{ if(largeHead == null){ largeHead = tmp; largetmp = largeHead; }else{ largetmp.next = tmp; largetmp =largetmp.next; } } tmp = tmp.next; if(smalltmp != null)smalltmp.next = null; if(largetmp != null)largetmp.next = null; } if(smalltmp != null)smalltmp.next = largeHead; return smallHead == null ? largeHead : smallHead;
0 0
- 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
- LeetCode - Partition List
- [LeetCode] Partition List
- LeetCode 86: Partition List
- LeetCode:Partition List
- 【leetcode】Partition List
- 图像放缩中最近邻插值和双线性插值的基本原理
- [CF81D] Polycarp's Picture Gallery && 贪心构造
- C语言大数相加
- Gallery的简单实用和Gallery的没落以及网上大牛给出的HorizontialListView水平方向ListView
- 关键字final,static break,return的应用
- Leetcode - Partition List.
- grid学习总结
- [CF486E] LIS of Sequence && DP
- UVA 11455 || Behold my quadrangle(三条边相加小等于最长边,不能组成四边形
- DSP TMS320F2812验证平台制作日志3
- C 语言中的指针和内存泄漏
- 近200篇机器学习&深度学习资料分享(含各种文档,视频,源码等)
- poi知识积累
- webdriver一