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的值分成两部分。详细思路和代码例如以下:

/**
* 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按X分成两段
* 小于的连接p
* 大于的连接q
* 最后合并p和q就可以
*/
ListNode p = new ListNode(0);
ListNode pHead = p;
ListNode q = new ListNode(0);
ListNode qHead = q;
//遍历
while(head != null){
if(head.val < x){//<x成一组
p.next = head;
p = p.next;
}else{//>=x成一组
q.next = head;
q = q.next;
}
head = head.next;
}
p.next = qHead.next;
q.next = null;//斩断后面的连接
return pHead.next;
}
}

leetCode 86.Partition List(分区链表) 解题思路和方法的更多相关文章

  1. leetCode 61.Rotate List (旋转链表) 解题思路和方法

    Rotate List  Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...

  2. LeetCode 86. Partition List 划分链表 C++

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  3. [leetcode]86. Partition List划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  4. [LeetCode] 86. Partition List 划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  5. [LeetCode]86. Partition List分离链表

    /* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...

  6. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  7. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  8. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  9. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

随机推荐

  1. Educational Codeforces Round 38 部分题解

    D. Buy a Ticket 分析 建一个源点,连向所有结点,边的花费为那个结点的花费,图中原有的边花费翻倍,最后跑一遍最短路即可. code #include<bits/stdc++.h&g ...

  2. (转)Ubuntu 16.04 安裝Docker(PS:本文适用amd64位的ubuntu系统)

    1.前置安裝,確保你的系統是64位 $ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-p ...

  3. elasticsearch中ik词库配置远程热加载

    1. 修改 IKAnalyzer.cfg.xml 配置文件中的<entry key="remote_ext_dict">http://127.0.0.1/xxx.txt ...

  4. [HDU1290]献给杭电五十周年校庆的礼物

    [HDU1290]献给杭电五十周年校庆的礼物 题目大意: 问\(n(n\le1000)\)个平面能够将一个三维空间分成几部分. 思路: 公式\(\frac{n^3+5n+6}6\). 源代码: #in ...

  5. OpenAPI安全防护

    1,开放API可能存在的数据安全问题 (1)数据窃取 通常体现为:钓鱼网站,拦截,伪装,截包 (2)数据篡改 中间被拦截,以代理的方式拦截数据,修改数据 (3)数据泄露 爬虫抓取核心数据 2,解决数据 ...

  6. 51NOD 1179 最大的最大公约数 筛法

    1179 最大的最大公约数 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1179 Description ...

  7. kali 执行apt-get upgrade后,终端无法打开的解决办法

    今天在kali执行apt-get upgrade命令后,reboot启动,发现进入界面终端无法开启 一波百度,google发现大概应该是语言的配置问题,因为最开始安装kali的时候是选择中文,可能up ...

  8. redis配置参数简介

    redis配置查看方式: 1.redis的安装目录查看redis.conf 2.登陆redis客户端,使用 config get xx命令. 比如:查看所有的配置: config get * [roo ...

  9. Regulator IC forms convenient overvoltage detector

    Figure 1 shows a simple, stand-alone overvoltage detector. The intent of the circuit is to monitor a ...

  10. Spring工具类ToStringBuilder用法简介

    比如说我们需要打印某个方法的User参数对象 package test; /** * * @author zhengtian * @time 2012-6-28 */ public class Use ...