leetcode JAVA Partition List 难度系数3 3.30
来源:互联网 发布:炫浪网络社区不能看了 编辑:程序博客网 时间:2024/05/21 09:58
Question:
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; * next = null; * } * } */public class Solution { public ListNode partition(ListNode head, int x) { ListNode res = new ListNode(-1); res.next = head; ListNode pre = res; ListNode post = new ListNode(-1); ListNode pCur = post; while(head!=null){ if(head.val<x){ pre.next=head; pre=head; }else {pCur.next = head;pCur=head;} head = head.next; } pCur.next=null; pre.next=post.next; return res.next; }}
0 0
- leetcode JAVA Partition List 难度系数3 3.30
- leetcode JAVA Rotate List 难度系数3 3.20
- leetcode JAVA Permutations 难度系数3 3.14
- leetcode JAVA Anagrams 难度系数3 3.15
- leetcode JAVA Combinations 难度系数3 3.26
- leetcode JAVA Subsets 难度系数3 3.27
- leetcode JAVA Triangle 难度系数3 3.42
- leetcode JAVA Remove Duplicates from Sorted List II 难度系数3 3.29
- leetcode JAVA Flatten Binary Tree to Linked List 难度系数3 3.40
- leetcode JAVA Combination Sum 难度系数3 3.13
- leetcode JAVA Pow(x, n) 难度系数3 3.16
- leetcode JAVA Maximum Subarray 难度系数3 3.17
- leetcode JAVA Jump Game 难度系数3 3.18
- leetcode JAVA Spiral Matrix II 难度系数3 3.19
- leetcode JAVA Unique Paths II 难度系数3 3.21
- leetcode JAVA Minimum Path Sum 难度系数3 3.22
- leetcode JAVA Simplify Path 难度系数3 3.23
- leetcode JAVA Set Matrix Zeroes 难度系数3 3.24
- “蓝桥杯”练习题中《基础练习 数列特征》的错误
- 九度oj 题目1388:跳台阶
- 设计模式之---------模板方法模式
- Codeforces Round #227 (Div. 2) B. George and Round
- 如果你没有选择离开
- leetcode JAVA Partition List 难度系数3 3.30
- JAVA中的异常分类
- VMware ESXi 5 安装
- C#添加C项目以及其dll引用不成功
- 九度oj 题目1538:GrassLand密码
- <option >怎么插入空字符
- hdu 4405 Aeroplane chess
- 九度oj 题目1525:子串逆序打印
- Codeforces #228 div1