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 ...
随机推荐
- java.lang.OutOfMemoryError: Java heap space错误及处理办
默认方式启动Eclipse时,有关启动时JVM参数是在Eclipse安装目录下的eclipse.ini文件中指定的.在命令行下,也可以通过参数-vmargs来达到此目的.其命令格式为:eclipse ...
- scrapy 知乎的模拟登陆及抓取用户数据
最近看了python的scrapy 框架并用其抓取了部分知乎用户数据,代码主要是集中在知乎登陆和抓取时候的逻辑处理上. 1. 首先进入知乎登陆页面zhihu.com/#sigin上, 用xpath提取 ...
- 高效PHP程序必知的53个技巧
用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说 ...
- socket的accept函数解析
今天与同学争执一个话题:由于socket的accept函数在有客户端连接的时候产生了新的socket用于服务该客户端,那么,这个新的socket到底有没有占用一个新的端口? 讨论完后,才发现,自己虽然 ...
- sublime text2教程
代码编辑器或者文本编辑器,对于程序员来说,就像剑与战士一样,谁都想拥有一把可以随心驾驭且锋利无比的宝剑,而每一位程序员,同样会去追求最适合自己的强大.灵活的编辑器,相信你和我一样,都不会例外. 我用过 ...
- Rancher安装使用
官网 http://docs.rancher.com/rancher/latest/en/quick-start-guide/#add-hosts 安装步骤: 1 Start up a Linux m ...
- .Net Core 中使用AutoMapper
1.新建一个类 using AutoMapper; using YourModels; using YourViewModels; namespace YourNamespace { public c ...
- Vultr免费vps注册和使用简易教程
如果你是站长,寻找托管网站的主机,或者是开发者,需要搭建服务器环境,选购vps是必须的.强烈不推荐国内的vps产品,没有性价比,维护水平又烂,甚至某些国内所谓云主机vps安装后门,监控你的数据.海外v ...
- Java 容器一些知识
一.Collection 1.static 方法: Collections.sort(List<T>):实现List排序功能 Collections.fill(List<T> ...
- webservice整合spring
接口HelloWorld需要添加webservice注解 package com.cs.webservice; import java.util.List; import javax.jws.WebP ...