一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        if(head==NULL||head->next==NULL) return head;
        ListNode* lessTail = head;//小于x部分的尾节点
        ListNode* p = head;
        ListNode* pre = NULL;//p前面的节点
        bool isFirst = false;//如果头节点就大于x为true,反之false
        bool needToChange = false;//p前面的节点都小于x,则不需要改变,反之需要进行插入操作
        if(head->val >= x) {isFirst = true;needToChange = true;}//如果第一个数就大于x,需要改变头节点
        while(p!=NULL)
        {
            if(p->val < x)
            {
                if(isFirst)//需要改变头节点
                {
                    ListNode* temp = p->next;
                    pre->next = p->next;
                    p->next=head;
                    lessTail = p;
                    head = p;
                    p = temp;
                    isFirst = false;
                }
                else
                {
                    if(!needToChange)//前面部分都小于x,则不需要改变
                    {
                        lessTail = p;
                        pre = p;
                        p=p->next;
                    }
                    else//需要进行插入操作
                    {
                        ListNode* temp = p->next;
                        pre->next = p->next;
                        p->next = lessTail->next;
                        lessTail->next = p;
                        lessTail = p;//注意要改变小于x部分的尾节点
                        p = temp;
                    }
                }
            }
            else
            {
                pre = p;
                p=p->next;
                needToChange = true;
            }
        }
        return head;
    }
};

后记:在这里祝大家端午节快乐,也给端午节还在坚持刷题的我自己点个赞。哈哈!

【一天一道LeetCode】#86. Partition List的更多相关文章

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

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

  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 划分链表 C++

    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 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分离链表

    /* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...

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

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

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

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

随机推荐

  1. Oracle中打印99乘法表的13种方法

    --实现1: select r1 || '*' || r1 || '=' || r1 * r1 A, decode(r2, '', '', r2 || '*' || r1 || '=' || r2 * ...

  2. Python List insert()方法详解

    1.功能insert()函数用于将指定对象插入列表的指定位置. 2.语法list.insert(index, obj) 3.参数index: 对象obj需要插入的索引位置.obj: 插入列表中的对象. ...

  3. 使用python scipy.optimize linprog和lingo线性规划求解最大值,最小值(运筹学学习笔记)

    1.线性规划模型: 2.使用python scipy.optimize linprog求解模型最优解: 在这里我们用到scipy中的linprog进行求解,linprog的用法见https://doc ...

  4. geotrellis使用(四十)优雅的处理请求超过最大层级数据

    前言 要说清楚这个题目对我来说可能都不是一件简单的事情,我简单尝试. 研究 GIS 的人应该都清楚在 GIS 中最常用的技术是瓦片技术,无论是传统的栅格瓦片还是比较新颖的矢量瓦片,一旦将数据切好瓦片就 ...

  5. Git提交代码到远程服务器

    1.下载Git 不用说了,这个是必须的,也是最简单的步骤,地址如下: http://git-scm.com/download 这里会提供三个版本的下载地址,读者可以自行查找. 2.创建代码库 远程的代 ...

  6. admin的配置

    当我们访问http://127.0.0.1:8080/admin/时,会出现: 执行命令: 生成同步数据库的脚本:python manage.py makemigrations             ...

  7. OpenResty 操作cookies

    在or中简单的使用cookies 复杂的操作请使用 [lua_resty_cookies](https://github.com/cloudflare/lua-resty-cookie) 基本操作 获 ...

  8. python 函数运算先于单目运算

    >>> def f(): >>> -f() - 初一看,-f()比较陌生的样子,细想,这是合理的

  9. [Matlab]技巧笔记

    1.将字符串作为Matlab命令执行 md = 'dir'; eval(md); 2.将字符串作为系统命令执行 md = 'dir'; system(md); 3.使显示图像的坐标轴使用相同的度量单位 ...

  10. 我的第一个RootKit,支持XP、Vista、Win7、Win8 RTM 32位

    只有写过一个BootKit,才能比较深刻的理解其整个过程与机制,也能加深对Windows系统引导各个过程的熟悉和理解. 我写的这个bootkit,暂时还没想到一个比较好的名字,它 1.  支持xp到w ...