题目链接

  题目要求:

  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.

  该题利用dummy节点能极大方便编程,具体程序如下:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if(!head || !head->next)
return head; ListNode *dummy = new ListNode(INT_MIN);
dummy->next = head;
ListNode *parNode = dummy;
ListNode *preNode = nullptr, *curNode = nullptr;
while(parNode && parNode->val < x)
{
preNode = parNode;
parNode = parNode->next;
} parNode = preNode;
if(!parNode || !parNode->next)
{
head = dummy->next;
delete dummy;
dummy = nullptr;
return head;
} preNode = parNode->next;
curNode = preNode->next;
while(curNode)
{
if(curNode->val < x)
{
ListNode *nextPar = parNode->next, *nextCur = curNode->next;
parNode->next = curNode;
curNode->next = nextPar;
parNode = parNode->next; curNode = nextCur;
preNode->next = curNode;
}
else
{
preNode = preNode->next;
curNode = curNode->next;
}
} head = dummy->next;
delete dummy;
dummy = nullptr; return head;
}
};

LeetCode之“链表”:Partition List的更多相关文章

  1. LeetCode 单链表专题 (一)

    目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...

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

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

  3. Leetcode解题-链表(2.2.0)基础类

    1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø  规定好每个子Solution都要实现纯虚函数test做测试: Ø  提供了List ...

  4. 【算法题 14 LeetCode 147 链表的插入排序】

    算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...

  5. LeetCode 86. 分隔链表(Partition List)

    86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...

  6. LeetCode OJ: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 链表 Partition List

    Partition List Total Accepted: 19761 Total Submissions: 73252My Submissions Given a linked list and ...

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

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

  9. [Swift]LeetCode86. 分隔链表 | Partition List

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

随机推荐

  1. 在一维坐标轴上有n个区间段,求重合区间最长的两个区间段。

    //重叠区间数 #define N 2 typedef struct arrange { int x; int y; } Arrange; //先按左边界排序,若相等再按右边界排序(升序) int c ...

  2. android 缓存实现

    1.之前因为做一个项目的过程中遇到要频繁重复下载的文件比如图片等,需要在本地缓存,除了用户体验也保证了省流量. 这个demo是用下载网络图片来演示. 一共有六张网络图片,加载图片时,会判断图片是否下载 ...

  3. Apache commons email 使用过程中遇到的问题

    apache-commons-email是对mail的一个封装,所以使用起来确实是很方便.特别的,官网上的tutorial也是极其的简单.但是我也仍然是遇到了没有解决的问题. jar包的添加 mail ...

  4. EJB3+JBoss5+Myeclipse9创建HelloWorld实例

    本实例用到的工具 1. jboss5 (配置不做介绍,谷歌度娘都有) 2. MyEclipse 9 实例创建 1.EJB类创建 打开MyEclipse 9 中右上角如下选项  再新建一个EJB项目 新 ...

  5. 3.QT事件处理,消息过滤器

     1  新建一个项目:06Event 新建cpp文件 06Event.pro HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += wid ...

  6. sublime text3空格和tab的显示

    最近在使用sublime text3修改shell文件时,明明看着相同的文件,对比却说不一样.最后发现是空格和tab惹的祸. 1.显示空格和tab: 在Preferences→Key Bindings ...

  7. 登录ssh时Host key verification failed错误

    工作中总是测试不同的路由设备, 路由器的ip都是 192.168.111.1 ,ssh登录的时候总是出现这个错误. macos上,错误如下 spawn ssh -p 22 root@192.168.1 ...

  8. Java进阶(四十)Java类、变量、方法修饰符讲解

    Java进阶(四十)Java类.变量.方法修饰符讲解 Java类修饰符 abstract: 将一个类声明为抽象类,没有实现的方法,需要子类提供方法实现. final: 将一个类生命为最终(即非继承类) ...

  9. Java并发框架——AQS阻塞队列管理(二)——自旋锁优化

    看Craig, Landin, and Hagersten发明的CLH锁如何优化同步带来的花销,其核心思想是:通过一定手段将所有线程对某一共享变量轮询竞争转化为一个线程队列且队列中的线程各自轮询自己的 ...

  10. springMVC源码分析--国际化实现Session和Cookie(二)

    上一篇博客springMVC源码分析--国际化LocaleResolver(一)中我们介绍了springMVC提供的国际化的解决方案,接下来我们根据springMVC提供的解决方案来简单的实现一个多语 ...