Partition List 链表划分区间@LeetCode
来源:互联网 发布:旅游拼车软件 编辑:程序博客网 时间:2024/06/10 07:04
简单的partition,用两个链表,3个指针。
package Level4;import Utility.ListNode;/** * 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 class S86 {public static void main(String[] args) {}public ListNode partition(ListNode head, int x) { ListNode l1 = new ListNode(-1);// 构建dummy链表头,l1存放所有比x小的节点 ListNode l2 = new ListNode(-1); ListNode cur = head; ListNode l1cur = l1; ListNode l2cur = l2; while(cur != null){ if(cur.val < x){ l1cur.next = new ListNode(cur.val); l1cur = l1cur.next; }else{ l2cur.next = new ListNode(cur.val); l2cur = l2cur.next; } cur = cur.next; } l1cur.next = l2.next;// 把两个链表合并起来 return l1.next; }}
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode partition(ListNode head, int x) { ListNode leftHead = new ListNode(-1); ListNode rightHead = new ListNode(-1); ListNode left = leftHead, right = rightHead; while(head != null) { if(head.val < x) { left.next = head; left = left.next; } else { right.next = head; right = right.next; } head = head.next; } right.next = null; // Notice we need to cut off the ring left.next = rightHead.next; return leftHead.next; }}
0 0
- Partition List 链表划分区间@LeetCode
- LeetCode(Partition List) 划分链表
- Leetcode 86. Partition List(链表划分)
- 【LeetCode】Partition List 链表划分
- LeetCode OJ 之 Partition List (划分链表)
- leetcode 86. Partition List-链表划分|双指针
- Leetcode 86. Partition List 链表划分 解题报告
- LeetCode 86. Partition List 链表的划分 Python Solution
- LintCode Partition List 链表划分
- LeetCode2.2.3 @ Partition List 链表划分 D3F3
- 96.Partition List-链表划分(容易题)
- LeetCode | Partition List(partition方法处理链表)
- Leetcode Partition List 分割链表
- leetcode Partition List二分链表问题
- LeetCode Partition List(链表分段)
- leetcode:Partition List (链表处理)【面试算法题】
- LeetCode Partition List 按值分段链表 系统分析
- 闲话链表(三) leetcode之Partition List
- N-Queens
- 小公司技术管理者的点滴--挽留员工
- C语言实现线性表之顺序存储结构操作
- Java -- JDBC 获取数据库自动 生成的主键值
- Windows Runtime 基础
- Partition List 链表划分区间@LeetCode
- POJ 2299 Ultra-QuickSort 求逆序对数(归并排序,树状数组)
- 归并排序中对小数组采用插入排序
- Android NDK纯C++开发(1)
- Unique Paths II
- 在sql plus中只知sys用户密码情况下如何切换到任意用户。
- category extension protocol(类目,延展,协议)
- 算法之旅,直奔<algorithm>之九 count
- 《惊天魔盗团》—电影随评