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 ...
随机推荐
- #Eureka 客户端和服务端间的交互
Eureka 服务器客户端相关配置 1.建立eureka服务器 只需要使用@EnableEurekaServer注解就可以让应用变为Eureka服务器,这是因为spring boot封装了Eureka ...
- Django 1.8 - “No migrations to apply” when run migrate after makemigrations 解决办法
解决办法 1 删除应用migrations目录 2 删除MySQL中django_migrations中对应的行(delete from django_migrations where app='ap ...
- Java Web 开发环境快速搭建
Java Web 开发环境快速搭建 在因某种原因更换开发设备后,可依据此文快速搭建开发环境,恢复工作环境. Java开发环境: Windows 10 (64-bit) Oralce JDK Eclip ...
- UVa 1600 Patrol Robot (习题 6-5)
传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...
- 【Python】生成器、回溯和八皇后问题
八皇后问题: 把N个皇后,放在N*N的棋盘上面,从第一行往下放,每个皇后占一行,同时,每个皇后不能处在同一列,对角线上,有多少种放置方法. 思路: 典型的回溯问题: 1.当要放置最后一个皇后时候,默认 ...
- 【Python@Thread】Semaphore&糖果机
信号量适用与多线程竞争有限资源的情况. from atexit import register from time import ctime, sleep from threading import ...
- shp文件显示
开发环境 Win7, VS2010 Sp1 QGIS 2.01 #include <qgsapplication.h> #include <qgsproviderregistry.h ...
- LeetCode OJ 62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 关于jquery选择器中:first和:first-child和:first-of-type的区别及:nth-child()和:nth-of-type()的区别
:first:选择第一个出现符合的元素 :first-child:选择限制条件中的第一个元素,并且必须和冒号前面的标签一致 :first-of-type:选择所有限制条件下的第一个冒号前面的标签元素, ...
- if语句—交互程序二
参考:<笨方法学Python>—习题31 用了两个嵌套的if语句! # coding: utf-8 print u"欢迎来到玩家国度, 你需要根据提示完成闯关!" # ...