Algorithms—86.Partition List
来源:互联网 发布:soc软件品牌 编辑:程序博客网 时间:2024/06/05 02:09
思路:按照要求,把节点分为2个list,递归。然后再合并。
/** * 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<ListNode> littleList=zuheL(head,new ArrayList<ListNode>(),x); List<ListNode> bigList=zuheB(head,new ArrayList<ListNode>(),x); int ll=littleList.size(); int lb=bigList.size(); if (ll==0||lb==0) { return head;} for (int i = 0; i < ll-1; i++) { littleList.get(i).next=littleList.get(i+1);} littleList.get(ll-1).next=bigList.get(0); for (int i = 0; i < lb-1; i++) { bigList.get(i).next=bigList.get(i+1);} bigList.get(lb-1).next=null; return littleList.get(0); } public List<ListNode> zuheL(ListNode head,List<ListNode> list,int x){ if (head!=null) { if (head.val<x) { list.add(head);}list=zuheL(head.next,list,x);} return list; } public List<ListNode> zuheB(ListNode head,List<ListNode> list,int x){ if (head!=null) { if (head.val>=x) { list.add(head);}list=zuheB(head.next,list,x);} return list; }}
耗时:312ms,中上游。
0 0
- Algorithms—86.Partition List
- Leetcode——86. Partition List
- [LeetCode]86.Partition List
- LeetCode 86. Partition List
- [Leetcode] 86. Partition List
- LeetCode --- 86. Partition List
- 86. Partition List
- [leetcode] 86.Partition List
- [leetcode] 86. Partition List
- 86. Partition List
- [LeetCode]86. Partition List
- 86. Partition List
- 86. Partition List LeetCode
- 86. Partition List
- 86. Partition List
- 86. Partition List
- Leetcode 86. Partition List
- LeetCode *** 86. Partition List
- 程序猿的活动
- LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
- 百度地图提供服务——本地搜索,范围搜索
- JNI编程
- 百度地图提供服务——本地搜索,范围搜索
- Algorithms—86.Partition List
- 【android】利用service监听来电或来信息
- Linux Socket编程
- gdb调试命令
- 树莓派Raspbian系统定制 - LXDE桌面系统定制 - 基本概念
- 解决svn执行cleanup出现failed–previous operation has not finished; run cleanup if it was interrupted 的错误
- Android地图添加标记和文字【代码片段】
- poj 1511 Invitation Cards(2遍spfa)
- 安卓开发第十二天