LeetCode Partition List(链表分段)
来源:互联网 发布:软件工程硕士院校排名 编辑:程序博客网 时间:2024/05/15 12:23
题意:给出一个单链表,有参数的数x。将比x小的数排在比x大的数据后面
思路:类似于快速排序的分段处理。为了简单操作,在单链表前加个头结点。注意,在处理时,如果当前处理结点与被替换结点为同一结点结,两个都下移,否则会出现环
代码如下:
class Solution{ public ListNode partition(ListNode head, int x) { if (null == head || null == head.next) return head; ListNode dummy = new ListNode(0); dummy.next = head; ListNode p = dummy, cur = dummy; while (cur != null && cur.next != null) { if (cur.next.val < x) { if (cur == p) { p = p.next; cur = cur.next; continue; } ListNode tmp = cur.next; ListNode next = p.next; cur.next = cur.next.next; p.next = tmp; tmp.next = next; p = p.next; continue; } cur = cur.next; } return dummy.next; }}
0 0
- LeetCode Partition List(链表分段)
- LeetCode Partition List 按值分段链表 系统分析
- LeetCode | Partition List(partition方法处理链表)
- Partition List 链表划分区间@LeetCode
- LeetCode(Partition List) 划分链表
- Leetcode Partition List 分割链表
- leetcode Partition List二分链表问题
- Leetcode 86. Partition List(链表划分)
- 【LeetCode】Partition List 链表划分
- leetcode:Partition List (链表处理)【面试算法题】
- 闲话链表(三) leetcode之Partition List
- LeetCode OJ 之 Partition List (划分链表)
- [LeetCode] Partition List(!!!!巧妙的链表插入)
- [LeetCode-86] Partition List (链表数据分区)
- leetcode 86. Partition List-链表划分|双指针
- LeetCode 86. Partition List(链表分区)
- Leetcode 86. Partition List 链表划分 解题报告
- LeetCode-86. Partition List (JAVA)分区链表
- 【HDU】 1719 Friend
- Java解释器模式
- 链表面试题之快慢指针问题(一) 查找链表中间节点
- Attribute "resource" must be declared for element type "mapper".
- NN & SN & DN
- LeetCode Partition List(链表分段)
- JavaCompiler
- ES是如何实现节点容灾的
- Java 集合类详解
- Android Studio混淆方法
- C++和双重检查锁定模式(DCLP)的风险
- android2.3.4----4.hal与framebuffer的交互
- 第16周项目-小玩文件(统计个数)
- Python - 标准库的使用方法