问题描述:

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.

算法分析:开始我只是想在原来链表上进行操作,但是无法返回头结点。这道题区别链表反转,链表反转不用新建链表,只用在原有的链表上操作就行了。这道题,要新建两个新的链表,一个链表的元素全部小于目标值,另一个链表的元素全部大于目标值。然后把这两个链表连接起来。

public ListNode partition(ListNode head, int x)
{
if(head == null || head.next == null)
{
return head;
}
ListNode lessHead = new ListNode(0);
ListNode greaterHead = new ListNode(0);
ListNode less = lessHead, greater = greaterHead;
ListNode node = head;
while(node != null)
{
ListNode temp = node.next;
if(node.val < x)
{
less.next = node;
less = less.next;
less.next = null;
}
else
{
greater.next = node;
greater = greater.next;
greater.next = null;
}
node = temp;
}
less.next = greaterHead.next;
return lessHead.next;
}

Partition List,拆分链表的更多相关文章

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

  2. [LeetCode] Partition List 划分链表

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

  3. 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 划分链表 C++

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

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

  6. 086 Partition List 分隔链表

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

  7. 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 划分链表

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

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

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

随机推荐

  1. react 将字符串解析为markdown

    react 中有许多组件支持把字符串转化为markdown,并以html的形式展示出来.这里我试用了 react-markdown 和 hyperdown 两个库,发现hyperdown比较好,因为r ...

  2. PHP error_log 新认知

    //error_log 简介及使用方法 //  error_log("消息","类型","路径"); //message   //type  ...

  3. Python的subprocess模块(二)

    原文:http://blog.chinaunix.net/uid-26000296-id-4461522.html 一.subprocess 模块简介 subprocess最早是在2.4版本中引入的. ...

  4. Mac 升级 OpenSSL

    [转载自 https://blog.csdn.net/focusjava/article/details/51179297 ] [升级Mac的openssl] 终端下 openssl version ...

  5. 手把手教你使用 GitBook

    一.简介 GitBook 是一个支持用 MarkDown 编写文档的软件,支持输出 HTML.PDF.eBook 格式文档.作为开发者我们一般会用它来写一些技术和接口文档. GitBook 官网:ht ...

  6. hibernate的事务和spring事务的区别 (转)

    spring事务: 对于传统的基于特定事务资源的事务处理而言(如基于JDBC的数据库访问),Spring并不会对其产生什么影响,我们照样可以成功编写并运行这样的代码.同时,Spring还提供了一些辅助 ...

  7. 懒加载 js----例子------图片

    转载自:https://www.jianshu.com/p/9b30b03f56c2 懒加载工具类 <script type="text/javascript"> // ...

  8. 20145316《Java程序设计》实验一:Java开发环境的熟悉(Windows + IDEA)

    20145316<Java程序设计>实验一:Java开发环境的熟悉(Windows + IDEA) 一.DOC命令行下Java程序开发 1.打开cmd,输入 mkdir 20145316命 ...

  9. windows安装git客户端

    1:线上git地址 https://github.com/ 2:tortoiseGit地址 http://tortoisegit.org 3:安装步骤 操作系统:Windows XP SP3 Git客 ...

  10. 5309 《Java程序设计》第6周学习总结

    教材学习内容总结 输入与输出 InputStream与OutputStream 从应用程序角度来看,如果要将数据从来源取出,可以使用输入串流:如果要将数据写入目的地,可以使用输出串流.在Java中,输 ...