问题描述:

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. golang处理 json中非法字符

    原文: Hi there, I just discovered Go and decided to port a little program to Go. The program reads JSO ...

  2. java组件学习15天

    Linuxdocker redminegitlabnginxmavenkibanakafkaspringspring cloudLucene 搭建基本使用

  3. python学习笔记(十三)接口开发

    一.开发接口的作用 1.mock接口,模拟一些接口,在别的接口没有开发好的时候,需要开发一些模拟接口进行调试和测试. 2.查看数据,比如,获取所有学员信息的接口,就不需要提供数据库的查看信息. 二.接 ...

  4. GlusterFS部署

    一.GlusterFS简介 PB级容量.高可用.读写性能.基于文件系统级别共享.分布式.无metadata(元数据)的存储方式. GlusterFS(GNU ClusterFile System)是一 ...

  5. Ubuntu apt-get更换阿里云源

    sudo vim /etc/apt/sources.list deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe ...

  6. socketserver 并发连接

    socketserver.TCPServer Example server side 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  7. Android中三种超实用的滑屏方式汇总(转载)

    Android中三种超实用的滑屏方式汇总   现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习惯性的有事没事的左右滑屏,也不知道在干什么...嘿嘿),由于 ...

  8. 微信小程序组件navigator

    导航navigator:官方文档 Demo Code: // redirect.js navigator.js Page({ onLoad: function(options) { this.setD ...

  9. 企业规范化管理系统iClap前生后世全解析

    iClap是一个产品管理系统,专注于为移动互联网企业提供企业规范化解决方案,改变传统的产品管理模式,实现产品管理场景化.APP.甚至原型图.效果图都可以直接进行批注和任务的协作,实时将你要修改的内容或 ...

  10. vuejs模仿实现一个电影分享类网站

    前几天朋友发了一个电影分享类网站:http://dy.bingthink.top/ 感觉界面还挺好看的,所以打算用vuejs模仿写一个. 从后台转前端,要学习的东西挺多的,仅以此记录我的学习历程,闲的 ...