Question:

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.

Tips:

给定一个无序的链表,将链表分成两部分,前半部分的val全部小于x;后半部分大于等于x。

前后两部分的结点,相对位置不变,即node1 与node2 均大于x 那么不管node1与node2的val谁大,最终顺序仍未node1->node2.

思路:

初始化两个头结点small 与big,然后开始遍历链表,当head.val>=x ,就将head接到big.next上。否则接到small.next上。

head走到末尾,将small的最后一个结点与big的第一个结点连接,返回small的第一个结点即可。

代码:

public ListNode partition(ListNode head, int x) {
if (head == null)
return head;
ListNode big = new ListNode(-1);
ListNode small = new ListNode(-1);
ListNode temp = big;// 记录big的第一个结点位置
ListNode result = small;// 记录small的第一个结点位置
while (head != null) {
if (head.val >= x) {
big.next = head;
big = big.next; } else {
small.next = head;
small = small.next;
}
head = head.next;
}
//small的最后一个结点与big的第一个结点连接
small.next = temp.next;
big.next = null;
return result.next;
}

测试代码:

public static void main(String[] args) {
L86PartitionList l86 = new L86PartitionList();
ListNode head1 = new ListNode(1);
ListNode head2 = new ListNode(4);
ListNode head3 = new ListNode(2);
ListNode head4 = new ListNode(5);
ListNode head5 = new ListNode(3);
ListNode head6 = new ListNode(6);
ListNode head7 = null;
head1.next = head2;
head2.next = head3;
head3.next = head4;
head4.next = head5;
head5.next = head6;
head6.next = head7;
ListNode head = l86.partition(head1, 30);
while (head != null) {
System.out.println(head.val);
head = head.next;
}
}

【Leetcode】86. Partition List的更多相关文章

  1. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  2. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  3. 【一天一道LeetCode】#86. Partition List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】86. 分隔链表

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

  5. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】763. Partition Labels 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...

  7. 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...

  8. 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  9. 【leetcode】1043. Partition Array for Maximum Sum

    题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...

随机推荐

  1. 20145234黄斐《java程序设计》实验四—Android程序设计

    实验过程: JDK.Android Studio的安装: 参见卢晓明博客:http://www.cnblogs.com/lxm20145215----/p/5444207.html 注意事项:解决Fe ...

  2. #400 – 使用ItemsPanel 属性将WrapPanel 作为ListBox的显示面板(Using a WrapPanel as the Items Panel for a ListBox)

    原文:#400 – 使用ItemsPanel 属性将WrapPanel 作为ListBox的显示面板(Using a WrapPanel as the Items Panel for a ListBo ...

  3. 北京Uber优步司机奖励政策(4月24日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. 【转载】COM 组件设计与应用(十八)——属性包

    原文:http://vckbase.com/index.php/wv/1265.html 一.前言 书接上回,本回着落在介绍属性包 IPersistPropertyBag 接口的实现方法和调用方式.属 ...

  5. SpringCloud-客户端的负载均衡Ribbon(三)

    前言:微服务架构,不可避免的存在单个微服务有多个实例,那么客户端如何将请求分摊到多个微服务的实例上呢?这里我们就需要使用负载均衡了 一.Ribbon简介 Ribbon是Netflix发布的负载均衡器, ...

  6. C#时间间隔

    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); stop ...

  7. 180815-Spring之RestTemplate中级使用篇

    Spring之RestTemplate中级使用篇 前面一篇介绍了如何使用RestTemplate发起post和get请求,然而也只能满足一些基本的场景,对于一些特殊的如需要设置请求头,添加认证信息等场 ...

  8. 我的第一个上线小程序,案例实战篇二——LayaAir游戏开始界面开发

    不知不觉我的第一个小程序已经上线一周了,uv也稳定的上升着. 很多人说我的小程序没啥用,我默默一笑,心里说:“它一直敦促我学习,敦促我进步”.我的以一个小程序初衷是经验分享,目前先把经验分享到博客园, ...

  9. Hyperledger Fabric 1.0 从零开始(一)

    在HyperLedger/Fabric发布0.6的时候,公司就已经安排了一个团队研究这一块,后来也请IBM的专家组过来培训了一批人,不幸的是,这批人后来全走了,然后1.0就发布了.自从2017年7月H ...

  10. oozie-ext

    安装oozie的时候需要ext的包支持,网站上找了一遍不是没有就是这个csdn下载还需要币,麻蛋...下面给出这个链接,在百度云上,如果失效了,在评论区或者给我留言,再发,一下是ext2.2.zip ...