问题描述:

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. linux伙伴系统接口alloc_page分析1

    在内核中分配内存,最后要通过伙伴系统接口进行实际物理页面的分配,一个重要的接口便是alloc_page.本文介绍下alloc_page的主要流程,各个部分的执行.主要包含正常分配流程,当页面不足的时候 ...

  2. nodejs获取参数的方法

    1 获取get的querystring参数 GET /test?name=fred&tel=0926xxx572 let aa = req.param("name"); l ...

  3. POJ1159:Palindrome(LCS小应用 回文)

    地址:http://poj.org/problem?id=1159 题目需求: 给你一个字符串,求最少添加多少字符可以使之构成回文串. 题目解析: 简单做法是直接对它和它的逆序串求最长公共子序列长度l ...

  4. math.h函数库

    C语言中之数学函数 C语言提供了以下的数学函数,要使用这些函数时,在程序文件头必须加入: #include <math.h> 编译时,必须加上参数「-lm」(表示连结至数学函式库),例如「 ...

  5. lua在线手册汇总

    1. Lua官方参考手册 Lua 4.0 : http://www.lua.org/manual/4.0/Lua 5.0 : http://www.lua.org/manual/5.0/Lua 5.1 ...

  6. Windows下IIS+PHP 5.2的安装与配置

    Windows下IIS+PHP 5.2的安装与配置   Windows下PHP的安装虽然简单,但如果不注意方法,仍然会让你头疼.此外,PHP 5.2版本与之前4.x版本也有一些不同,所以有必要记录一下 ...

  7. TWebBrowser静音

    procedure TForm1.FormCreate(Sender: TObject); var   hDSound: Cardinal;   pDirectSoundCreate: Pointer ...

  8. 84. Largest Rectangle in Histogram(直方图最大面积 hard)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  9. cdoj1587 失恋772002天

    地址:http://acm.uestc.edu.cn/#/problem/show/1587 题目: 失恋772002天 Time Limit: 3000/1000MS (Java/Others)   ...

  10. Java-线程池专题(什么是线程池,如何使用,为什么要用)

    1.什么是线程池:  java.util.concurrent.Executors提供了一个 java.util.concurrent.Executor接口的实现用于创建线程池 多线程技术主要解决处理 ...