原题地址:

https://oj.leetcode.com/problems/partition-list/

题目内容:

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.

方法:

链表的基本功。

维护两个带头结点的链表,一个存 < ,一个存 >=,然后一个连接就可以了。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode partition(ListNode head, int x) {
if (head == null)
return null;
ListNode lessHead = new ListNode(0);
ListNode moreHead = new ListNode(0);
ListNode lessTail = lessHead;
ListNode moreTail = moreHead;
ListNode tmp = null;
while (head != null)
{
tmp = head;
head = head.next;
tmp.next = null;
if (tmp.val < x)
{ lessTail.next = tmp;
lessTail = lessTail.next;
}
else
{
moreTail.next = tmp;
moreTail = moreTail.next;
}
}
if (lessHead.next != null)
lessTail.next = moreHead.next;
else
lessHead.next = moreHead.next;
return lessHead.next;
}
}

【原创】leetCodeOj ---Partition List 解题报告的更多相关文章

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

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

  2. 【原创】leetCodeOj --- Min Stack 解题报告

    题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...

  3. 【原创】leetCodeOj --- Dungeon Game 解题报告

    原题地址: https://oj.leetcode.com/problems/dungeon-game/ 题目内容: The demons had captured the princess (P) ...

  4. 【原创】leetCodeOj --- Largest Number 解题报告

    原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...

  5. 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

    题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...

  6. 【原创】leetCodeOj --- Sort List 解题报告

    今日leetcode链表题全制霸 原题地址: https://oj.leetcode.com/problems/sort-list/ 题目内容: Sort List Sort a linked lis ...

  7. 【原创】leetCodeOj --- Interleaving String 解题报告

    题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3  ...

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

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

  9. LeetCode: Partition List 解题报告

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

随机推荐

  1. delphi 回调函数

    program Project2; {$APPTYPE CONSOLE} uses SysUtils; type //定义一个对象事件方法 TCallbackFunc = function (i: I ...

  2. eclipse设置关联文件打开方式

    window->preferences: General->Editors->File Associations

  3. Apache Commons IO入门教程(转)

    Apache Commons IO是Apache基金会创建并维护的Java函数库.它提供了许多类使得开发者的常见任务变得简单,同时减少重复(boiler-plate)代码,这些代码可能遍布于每个独立的 ...

  4. That's life,多一些韧性,才有更多的任性(转)

    如果是正确的选择,就不要遵守太多规则. 若有容纳之心,便丰富了自己,也闪了他人,平常心,平常事 阅读,是保持时尚最节约的方式,也是快乐的源泉.可人生难免失意,有了快乐的能力,还应有面对沮丧的心胸. 相 ...

  5. SLB 权重问题

    <pre name="code" class="html">一般配置SLB的时候有个权重0到100,是如何选择数值的? 权重需要您根据后端机器的配置 ...

  6. 介绍一个C++奇巧淫技

    你能实现这样一个函数吗:   MyType type;   HisType htype;   serialize_3(11, type, htype);   serialize_4(type, hty ...

  7. FZU2177(dp)

    传送门:ytaaa 题意:有n个***(不能调换顺序),可以组成x(x<n)个炸弹,每个炸弹的威力为该组的(max-min)^2,现在给出n个***的威力值,求能组成所有炸弹的最大威力和. 分析 ...

  8. android4.3环境搭建

    方案一: 首先android环境搭建有如下几个东西是必须准备的: 1.  Eclipse (下载地址:http://www.eclipse.org/downloads/,建议至少3.4及以上版本) 2 ...

  9. [ACM] poj 1258 Agri-Net (最小生成树)

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37131   Accepted: 14998 Descri ...

  10. Androidclient和server端数据交互的第一种方法

    网上有非常多样例来演示Android客户端和server端数据怎样实现交互只是这些样例大多比較繁杂,对于刚開始学习的人来说这是不利的.如今介绍几种代码简单.逻辑清晰的交互样例,本篇博客介绍第一种: 一 ...