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.

Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5

题意:

给定一个链表和一个值,把小于等于和大于该值的部分分别放到链表的一前一后去。

思路:

先分为两个链表

然后合并

代码:

 class Solution {
public ListNode partition(ListNode head, int x) {
ListNode leftDummy = new ListNode(-1);
ListNode rightDummy = new ListNode (-1);
ListNode left_cur = leftDummy;
ListNode right_cur = rightDummy;
ListNode cur = head; while( cur != null){
if(cur.val < x){
left_cur.next = cur;
left_cur = cur;
}else{
right_cur.next = cur;
right_cur = cur;
}
cur = cur.next;
}
left_cur.next = rightDummy.next;
right_cur.next = null;
return leftDummy.next;
}

[leetcode]86. Partition List划分链表的更多相关文章

  1. [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 ...

  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分离链表

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

  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. [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. [LeetCode] Partition List 划分链表

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

  7. 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 ...

  8. partition List(划分链表)

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

  9. [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 ...

随机推荐

  1. tomcat访问错误调试方法

    生产环境中经常用到tomcat,所以还是要学一下tomcat的排错的 很重要的一点,就是实时查看catalina.out日志 执行tail -f catalina.out就会实时刷新日志了 catal ...

  2. Nginx调试入门

    1.查看nginx.conf配置文件是否有错误 ./nginx -t -c ./nginx.conf   #可以看到,正常情况下语法没问题,配置文件测试成功了,-t测试-c配置文件 如果我故意加入错误 ...

  3. Python生成器(generator)和迭代器(Iterator)

    列表生成式 a = [i+1 for i in range(10)] print(a) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 这就是列表生成式 生成器(generator) ...

  4. C# Windows Service 基础

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  5. [转]使用python爬取东方财富网机构调研数据

    最近有一个需求,需要爬取东方财富网的机构调研数据.数据所在的网页地址为: 机构调研 网页如下所示: 可见数据共有8464页,此处不能直接使用scrapy爬虫进行爬取,因为点击下一页时,浏览器只是发起了 ...

  6. Linux和Windows启动后台程序

    平时很多时候,我们需要通过脚本命令调用执行程序,集成一体后方便使用快捷.但是启动脚本窗口比较碍眼,能设置为后台运行既方便又美观. Linux启动后台程序 1.后台执行 nohup方法:不挂断的运行命令 ...

  7. WordPress版微信小程序2.4版发布

    自从发布2017年9月16日WordPress版微信小程序2.2.8版本后,这个一个多月来,WordPress版微信小程序,在经过一些比较小的更新后,今天发布阶段性的版本:2.4版 .这版本主要是功能 ...

  8. 性能监控扩展篇(grafana + influxdb + telegraf)

    之前已经说过了自己写sh脚本监控,我看有人评论了说用telegraf进行数据收集,于是乎去研究了下,感觉还可以,不过磁盘io的的表个人感觉有些美中不足,并未直接给出读写速率的情况,可能是研究时间太短, ...

  9. win10安装mysql一直卡在最后一步进行不下去

    新买的电脑,mysql的win10一直安装不了,一直卡在最后一步.仔细阅读下面文章解决. https://blog.csdn.net/fpga_zy/article/details/80689265

  10. Django Forms 表单

    环境 python 3.7 服务端  views.py from django import forms # 引入 froms 模块 from django.forms import widgets ...