leetCode 86.Partition List(分区链表) 解题思路和方法
来源:互联网 发布:锦官城桌游淘宝店 编辑:程序博客网 时间:2024/04/28 13:14
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
.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode partition(ListNode head, int x) { /** * 思路是将list按X分成两段 * 小于的连接p * 大于的连接q * 最后合并p和q即可 */ ListNode p = new ListNode(0); ListNode pHead = p; ListNode q = new ListNode(0); ListNode qHead = q; //遍历 while(head != null){ if(head.val < x){//<x成一组 p.next = head; p = p.next; }else{//>=x成一组 q.next = head; q = q.next; } head = head.next; } p.next = qHead.next; q.next = null;//斩断后面的连接 return pHead.next; }}
0 0
- leetCode 86.Partition List(分区链表) 解题思路和方法
- LeetCode 86. Partition List(链表分区)
- LeetCode-86. Partition List (JAVA)分区链表
- Leetcode 86. Partition List 链表划分 解题报告
- leetCode 61.Rotate List (旋转链表) 解题思路和方法
- leetCode 92.Reverse Linked List II (反转链表II) 解题思路和方法
- [LeetCode-86] Partition List (链表数据分区)
- [leetcode] 86. Partition List 解题报告
- [Leetcode] 86. Partition List 解题报告
- [leetcode]86. Partition List@Java解题报告
- LeetCode | Partition List(partition方法处理链表)
- leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的重复II) 解题思路和方法
- leetCode 83.Remove Duplicates from Sorted List(删除排序链表的重复) 解题思路和方法
- leetcode 114.Flatten Binary Tree to Linked List (将二叉树转换链表) 解题思路和方法
- [LeetCode]Partition List, 解题报告
- 【LeetCode】Partition List 解题报告
- [LeetCode] Partition List 解题报告
- 【LeetCode】Partition List 解题报告
- Timus Online Judge 1057. Amount of Degrees(数位dp)
- HDOJ 2001 计算两点间的距离(水题)
- PHP文件系统
- CLISP special
- 2015多校联合训练总结
- leetCode 86.Partition List(分区链表) 解题思路和方法
- nyoj 71 独木舟上的旅行 贪心
- POJ 1182 食物链
- bzoj4196: [Noi2015]软件包管理器
- POJO和javaBean
- 关于《一种鱼眼图象到透视投影图象的变换模型》
- cisco 2950密码恢复
- PAT (Advanced Level) 1040. Longest Symmetric String (25) 动态规划
- android5.0学习(一)