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.

class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if(!head || !(head->next) ) return head;
ListNode *current = head;
ListNode *smallPointer = NULL; //point to the last node <x
ListNode *largePointer = NULL; //point to the last node >x
while(current)
{
if(current->val >= x)
{
largePointer = current;
current = current->next;
}
else
{
if(!largePointer)
{
smallPointer = current;
current = current->next;
}
else if(smallPointer)
{
largePointer->next = smallPointer->next;
smallPointer -> next = current;
current = current->next;
smallPointer = smallPointer->next;
smallPointer->next = largePointer->next;
largePointer->next = current;
}
else //head
{
smallPointer = current;
current = current->next;
smallPointer->next = head;
head = smallPointer;
largePointer->next = current;
}
}
}
return head; }
};

86. Partition List (List)的更多相关文章

  1. leetcode 143. Reorder List 、86. Partition List

    143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...

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

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

  3. 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 ...

  4. 86. Partition List

    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. LeetCode OJ 86. Partition List

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

  7. 【一天一道LeetCode】#86. Partition List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 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 ...

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

  10. 【Leetcode】86. Partition List

    Question: Given a linked list and a value x, partition it such that all nodes less than x come befor ...

随机推荐

  1. ESP8266编译过程探索

    首先看到的是为什么SDK里有一个libmain.a这样的库. 编译之后才发现原平是这样子:main函数(如果有的话)会调用我们user_init函数.user_rf_cal_sector_set函数, ...

  2. 【译】GNU Radio How to write a block 【如何开发用户模块及编写功能块】

    本文讲解如何在GNU Radio中添加用户开发的信号处理模块,译文如有不当之处可参考原文地址:http://gnuradio.microembedded.com/outoftreemodules Ou ...

  3. pycaffe + anaconda2 + python2.7.配置

    1.首先要把caffe-windows用VS2013编译好(这一步很多门道,很麻烦很多坑),编辑props文件,使python支持选项开启,单独编译pycaffe项目就可以 2.将生成的在build/ ...

  4. OpenSSL生成公钥私钥***

    证书标准 X.509 - 这是一种证书标准,主要定义了证书中应该包含哪些内容.其详情可以参考RFC5280,SSL使用的就是这种证书标准. 编码格式 同样的X.509证书,可能有不同的编码格式,目前有 ...

  5. 黄聪:WordPress默认编辑器可视化切换不见了,非插件导致消失问题

    1.后台---用户---我的个人资料 2.看看 [可视化编辑器]的[撰写文章时不使用可视化编辑器]项目是不是勾上了 3.去掉保存即可

  6. DS03--栈和队列

    一.学习总结 1 关键词: 逻辑结构,存储结构,抽象数据类型,顺序存储类型,链式存储类型,线性表应用 栈和队列 2 使用思维导图将这些关键词组织起来. 二.PTA实验作业 2.1题目1:符号配对 请编 ...

  7. 【JS】if...else 优化形式

    if () {} else {} —— 使用三元操作符/省略大括号{} if(foo){ funcA(); }else{ funcB(); } foo?funcA():funcB(); if(!foo ...

  8. java代码-----循环变量的

    总结:输出相同的结果,很可能就是-个只是赋初始值, package com.mmm; public class Pnal { public static void main(String[] args ...

  9. vs2010 安装 Ajax Control Toolkit

    Ajax Control Toolkit 7.1005.0 The Ajax Control Toolkit contains a rich set of controls that you can ...

  10. python学习——练习题(12)

    """ 题目:判断101-200之间有多少个素数,并输出所有素数. 质数(prime number)又称素数,有无限个. 质数定义为在大于1的自然数中,除了1和它本身以外 ...