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. RelativeLayout用到的一些重要的属性

    第一类:属性值为true或false android:layout_centerHrizontal                                           水平居中    ...

  2. gulp 制作雪碧图

    雪碧图:sprite 是把多张图片拼到一张图中,提升性能的一种做法.把合并的图片一次性加载到内存中,需要时只渲染一部分. 我们选择gulp.spritesmith插件. 使用gulp时首先要在指定的任 ...

  3. php部分--例子:租房子(复选框的全选、数组拼接成字符串、设置复选框的name值、)

    1.链接数据库 <?php include("DBDA.class.php"); $db=new DBDA(); $sql="select * from fangz ...

  4. JavaWeb学习记录(五)——Servlet随机产生验证码

    随机产生验证码的工具类: import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;impo ...

  5. dictEntry **table;

    typedef struct dictht { dictEntry **table; PORT_ULONG size; PORT_ULONG sizemask; PORT_ULONG used;} d ...

  6. MySQL数据库InnoDB存储引擎多版本控制(MVCC)实现原理分析

    文/何登成 导读:   来自网易研究院的MySQL内核技术研究人何登成,把MySQL数据库InnoDB存储引擎的多版本控制(简称:MVCC)实现原理,做了深入的研究与详细的文字图表分析,方便大家理解I ...

  7. EasyUI之DataGrid使用

    http://blog.csdn.net/liovey/article/details/9173931 背景介绍: 原 先项目采用普通的jsp页面来做为前端显示,用户体验差,并且为了实现某一种效果需要 ...

  8. for 与 foreach 性能

    For 与Foreach 性能 差别在不同的场景下会有不同的差异. 对于不同的目标  , 如 T[] 与 IEnumerable<T> 两个的性能就感觉出来了,对于T[] 都快.     ...

  9. linux包之包管理命令rpm-yum

    背景 YUM(Yellow dog Updater, Modified)为多个Linux发行版的前端软件包管理器,例如 Redhat RHEL, CentOS & Fedora. YUM通过调 ...

  10. unity, 搜索组件

    Hierarchy的搜索栏中既可以搜节点名,也可以搜组件名.