原题地址:

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. Java-WebSocket 项目的研究(三) WebSocketClient 类 具体解释

    通过之前两篇文章 Java-WebSocket 项目的研究(一) Java-WebSocket类图描写叙述 Java-WebSocket 项目的研究(二) 小试身手:client连接server并发送 ...

  2. cocos2d-x项目101次相遇: Scenes , Director, Layers, Sprites

    cocos2d-x 101次相遇 / 文件夹  1   安装和环境搭建 -xcode  2   Scenes , Director, Layers, Sprites 3   建立图片菜单  4   在 ...

  3. ftk学习记(waitbox篇)

    [声明:版权全部.欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 前面说到了脚本.那么就看看ftk中demo与script搭配的效果是什么样的? 上面的效果图就相 ...

  4. javascript它【蛇系列】第一弹:简单的蛇实现

    参考博客:http://blog.csdn.net/sunxing007/article/details/4187038 上面的博客是一个参考,竟第一次做.真让自己盲人摸象做不出来. 只是我在其上做了 ...

  5. 30第二建筑Github Page

    从我原来博客的前端传输.链接:http://www.hacke2.cn/create-github-page/ 假设中国每一个程序猿都写博客,那么中国IT届的春天就来了 有同学问我的站点是怎么创建的, ...

  6. dsbskrhkme看么哦么

    http://pan.baidu.com/share/link?shareid=3011665141&uk=338692646&third=15                http ...

  7. Codeforces Round #253 DIV1 C 馋

    http://codeforces.com/contest/442/problem/C 题意非常easy,基本上肯定有坑坑洼洼的样子.看题目案例,从第三个跟第二个没有凹的案例来看的话,多写几个以及多画 ...

  8. C#之异步编程

    1 异步编程的重要性 C#5.0最重要的改进是提供了更强大的异步编程,C#5.0仅增加两个关键字Async和Await,使用异步编程,方法调用是后台运行(通常在线程和任务的帮助下),并且不会阻塞调用线 ...

  9. 天嵌E9平台下进行USB Wifi模块RT3070驱动的移植

    因为项目工作须要,要在天嵌E9平台下实现wifi的点对点,点对多点的传输. Wifi 模块芯片:雷凌3070芯片 嵌入式平台:E9(ARM) 交叉编译环境:arm-none-linux-gnueabi ...

  10. @font-face(css3属性)实如今网页中嵌入随意字体

    @font-face语法规则 @font-face { font-family: <YourWebFontName>; src: <source> [<format> ...