LeetCode OJ 86. 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
.
刚开始思考这个问题时绕了点弯路,其实很简单,只要把链表中比x小的节点拆下来按顺序链接到一起构成list1,然后把剩下的节点按顺序链接到一起构成list2。最后list1.next=list2即可。代码如下:
public class Solution {
public ListNode partition(ListNode head, int x) {
if(head==null || head.next==null) return head; ListNode Partition = head; ListNode nhead = new ListNode(0);
nhead.next = null;
ListNode ntail = nhead; ListNode nhead2 = new ListNode(0);
nhead2.next = null;
ListNode ntail2 = nhead2; ListNode p = null;
while(Partition!=null){
if(Partition.val < x){
p = Partition.next;
ntail.next = Partition;
ntail = ntail.next;
ntail.next = null;
Partition = p;
}
else{
p = Partition.next;
ntail2.next = Partition;
ntail2 = ntail2.next;
ntail2.next = null;
Partition = p;
}
}
ntail.next = nhead2.next;
return nhead.next;
}
}
LeetCode OJ 86. Partition List的更多相关文章
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【一天一道LeetCode】#86. Partition List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【Leetcode】86. Partition List
Question: Given a linked list and a value x, partition it such that all nodes less than x come befor ...
- LeetCode OJ:Partition List(分割链表)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
- Leetcode OJ 刷题
Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
随机推荐
- vue router 只需要这么几步
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Unity5权威讲解
Photon Cloud 299c7416-a08d-4a23-95a1-e4be108259aa Shooter 视频:https://pan.baidu.com/s/1kVFJ1x9 项目:htt ...
- 通过SvcUtil.exe 生成 Wcf 客户端代理
WCF服务调用通过两种常用的方式:一种是借助代码生成工具SvcUtil.exe或者添加服务引用的方式,一种是通过ChannelFactory直接创建服务代理对象进行服务调用. SvcUtil.exe ...
- BeautifulSoup抓取列表页锚文本
素闻BeautifulSoup提取效率低,艾玛,第一印象果然是很要命的,反正比Re 和 Lxml 是要慢的,不过就无奈Re的正则折腾来折腾去,没写出来,Lxml 的 Xpath 又用得不好. 不过就这 ...
- 第七十一,CSS颜色与度量单位
CSS颜色与度量单位 学习要点: 1.颜色表方案 2.度量单位 本章主要探讨HTML5中CSS颜色和度量单位等问题,包括颜色的选取方式.相对长度和绝对长度等. 一.颜色表方案 1 颜色的表现形式主 ...
- terminal color
自己喜欢的前背景颜色1: foreground: ab8d0f yellow c4a000 default background: 23292b ...
- post请求和get请求
get请求在链接后面带参数,容易出现乱码,是坑(慎用),有固定的长度 一般的用的就是post方式 <form action="<%=basePath%>upload&quo ...
- POJ 1845 Sumdiv#质因数分解+二分
题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想 ...
- Codeforces Round #366 (Div. 2)_B. Spider Man
B. Spider Man time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Python之列表&元组&字典
今天学习了Python的基本数据类型,做以下笔记,以备查用. 一.列表 列表的常用方法: 1.append()方法 def append(self, p_object): # real signatu ...