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.

==========

题目:

有一个数字链表和单独的值x,重新划分链表,

将链表中的所有小于x的节点放到  大于等于x的节点前面,

小于x的节点的相对顺序不变,大于等于x的节点的相对顺序不变.

--------------

思路:

和奇数偶数排序一样类似,

遍历链表的时候,将节点分组,

难点在于怎么判断开始?怎么判断链表结束?

定义两个假的头节点dummy1,dummy2,利用两个p1和p2分别指向前两个节点

外加一个遍历list的指针curr,

最后将dummy1链表的尾指向dummy2链表的头节点(这里dummy1和dummy2都是节点,不是指针).

代码如下:

/**
* 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==nullptr || head->next==nullptr) return head;
ListNode *head1,*head2;
ListNode *p1,*p2,*curr;
ListNode dummy1(-);
ListNode dummy2(-);
curr = head;
p1 = head1 = &dummy1;
p2 = head2 = &dummy2; while(curr){
if(curr->val < x){
p1->next = curr;
p1 = p1->next;
//p1->next = nullptr;
}else{
p2->next = curr;
p2 = p2->next;
//p2->next = nullptr;
}
curr = curr->next;
}
p1->next = nullptr;
p2->next = nullptr;
//showList(head1->next);
//showList(head2->next); p1->next = head2->next;
return head1->next;
}
};

86. Partition 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. [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 ...

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

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

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

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

  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. 【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. spring学习笔记---Jackson的使用和定制

      前言: JAVA总是把实体对象(数据库/Nosql等)转换为POJO对象再处理, 虽然有各类框架予以强力支持. 但实体对象和POJO, 由于"饮食习惯", "民族特色 ...

  2. 经典 socket通讯 -- 已验证

    [tcp通信] 来源:不详: client: package com.defonds.socket.begin; import java.io.BufferedReader; import java. ...

  3. PHP实例开发(1)PHP站内搜索

    PHP站内搜索:多关键字.加亮显示 1.SQL语句中的模糊查找 $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' a ...

  4. preventDefault()方法

    必须在dragend和dragover事件内调用“事件对象.preventDefault()”方法.因为在默认情况下,拖放的目标元素是不允许接受元素的,为了把元素拖放到其中,必须把默认处理给关掉.目前 ...

  5. 《Java程序设计》第3周学习总结

    学号20145220<Java程序设计>第3周学习总结 教材学习内容总结 使用jave撰写程序几乎都在使用对象(Object),要产生对象必须先定义类(Class),类是对象的设计图,对象 ...

  6. 局域网络ping不通

    描述:今天和老崔.老周去公司的新办公地点//相比临时的,十分高大上.当我们把两台台式电脑A.B装好了,网络设置也陪好了,确认能够上网,再装打印机的时候,发现搜索不到打印机的ip(打印机也是有自己的IP ...

  7. centos启动流程[转]

    启动流程概览 在硬件驱动成功后,Kernel 会主动呼叫 init 程序,而 init 会取得 run-level 资讯: init 运行 /etc/rc.d/rc.sysinit 文件来准备软件运行 ...

  8. php strtotime 和 date 日期操作

    time()得到的数值是1970-1-1到现在的秒数,strtotime("now")也是,两个值是相同的. http://doc.thinkphp.cn/manual/syste ...

  9. (转)The Road to TensorFlow

    Stephen Smith's Blog All things Sage 300… The Road to TensorFlow – Part 7: Finally Some Code leave a ...

  10. Debugging a Parallel Application

    Walkthrough: Debugging a Parallel Application https://msdn.microsoft.com/en-us/library/dd554943.aspx ...