给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。
你应当保留两个分区中每个节点的初始相对位置。
例如,
给定1->4->3->2->5->2 和 x = 3,
返回1->2->2->4->3->5.

详见:https://leetcode.com/problems/partition-list/description/

Java实现:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
if(head==null||head.next==null){
return head;
} ListNode small = new ListNode(-1);
ListNode newSmall = small;
ListNode big = new ListNode(-1);
ListNode newBig = big; while(head!=null){
if(head.val<x){
small.next = head;
small = small.next;
}else{
big.next = head;
big = big.next;
}
head = head.next;
}
big.next = null; small.next = newBig.next; return newSmall.next;
}
}

参考:https://www.cnblogs.com/springfor/p/3862392.html

086 Partition List 分隔链表的更多相关文章

  1. Leetcode86. Partition List分隔链表(双指针)

    给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1->4-&g ...

  2. LeetCode 86. 分隔链表(Partition List)

    86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...

  3. Leetcode 86.分隔链表

    分隔链表 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1-> ...

  4. 【LeetCode】86. 分隔链表

    86. 分隔链表 知识点:链表: 题目描述 给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前. 你应当 保留 两个 ...

  5. [CareerCup] 2.4 Partition List 划分链表

    2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...

  6. Java实现 LeetCode 725 分隔链表(暴力)

    725. 分隔链表 给定一个头结点为 root 的链表, 编写一个函数以将链表分隔为 k 个连续的部分. 每部分的长度应该尽可能的相等: 任意两部分的长度差距不能超过 1,也就是说可能有些部分为 nu ...

  7. [Swift]LeetCode86. 分隔链表 | Partition List

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

  8. LeetCode 86. 分隔链表(Partition List)

    题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1-> ...

  9. 力扣——Partition List(分隔链表) python实现

    题目描述: 中文: 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = ...

随机推荐

  1. codeforces A. Fox and Box Accumulation 解题报告

    题目链接:http://codeforces.com/problemset/problem/388/A 题目意思:有 n 个 boxes,每个box 有相同的 size 和 weight,但是stre ...

  2. tflearn 中文汉字识别,训练后模型存为pb给TensorFlow使用——模型层次太深,或者太复杂训练时候都不会收敛

    tflearn 中文汉字识别,训练后模型存为pb给TensorFlow使用. 数据目录在data,data下放了汉字识别图片: data$ ls0  1  10  11  12  13  14  15 ...

  3. swoole异步redis安装前置条件和流程

    ---恢复内容开始--- 1.redis服务 确认redis在服务器中已经安装了 2.hiredis库 第二步安装hiredis 下载位置 2.1获取 安装包https://github.com/re ...

  4. flume 日志收集单节点

    flume 是 cloudera公司研发的日志收集系统,采用3层结构:1. agent层,用于直接收集日志;2.connect 层,用于接受日志; 3. 数据存储层,用于保存日志.由一到多个maste ...

  5. Linux-正则表达式与三剑客

    1 固化命令文件 登录时执行文件的顺序 /etc/profile /etc/profile.d ~/.bash_profile ~/.bashrc /etc/bashrc 非登录shell ~/.ba ...

  6. 002 - 配置Pycharm的背景颜色为豆沙绿护眼

    本文记录的是Pycharm2017年1月版本 Pycharm有自带的主题可供选择, 好多小伙伴喜欢使用Monoka的风格 但是也有不是很喜欢黑底风格的小伙伴可以使用默认主题+豆沙绿来替代 默认主题是白 ...

  7. PHP 单引号与双引号的区别 SQL中的使用

    php单引号与双引号用法:引号嵌套方法 1.双引号内不能直接就再嵌套双引号 2.双引号与单引号互相嵌套使用 如: 双引号内直接嵌套单引号 echo "<script language= ...

  8. 24.如何结束返回值是void的方法

    如何结束返回值是void的方法? return;只能够出现在方法类型是void 的方法中,用来结束方法. return后面还可以跟数据,后面的数据可以是整数.字符串.false.ture.小数.主要看 ...

  9. PTA-数据结构 Dijkstra 城市间紧急救援

    城市间紧急救援(25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标 ...

  10. D - Bear and Finding Criminals

    Description There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long ro ...