一天一道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. ignorable tips

    枚举 索引从0开始 sort 默认升序排列  Array.Sort(intSort); //复制数组 Array.Copy(intSort,intNew,3); intsort 源数组 intnew ...

  2. jquery传值与判断

    js判断是否包含字符串 var str="Hello world!" var s = str.indexOf("Hello") 存在则s>-1不存在则是s ...

  3. Redis持久化的两种方式(RDB和AOF)

    redis提供了两种持久化的方式,分别是RDB(Redis DataBase)和AOF(Append Only File). RDB,简而言之,就是在不同的时间点,将redis存储的数据生成快照并存储 ...

  4. ACM Bee

    In Africa there is a very special species of bee. Every year, the female bees of such species give b ...

  5. centos 7安装pycharm

    1.首先安装jdk: yum install java 结果: [root@controller bin]# java -version openjdk version "1.8.0_131 ...

  6. PHP If...Else 语句

    PHP If...Else 语句 条件语句用于根据不同条件执行不同动作. PHP 条件语句 当您编写代码时,您常常需要为不同的判断执行不同的动作.您可以在代码中使用条件语句来完成此任务. 在 PHP ...

  7. Mongo Index

    摘要 mongo 的索引非常强大,和关系型数据库索引没什么区别.这里主要介绍本人在mongo索引上的犯的错. 索引种类 1.单字段索引 2.复合索引 多个字段索引 如{name:1,address:1 ...

  8. sublime text 2 解决错误 [Decode error - output not utf-8]

    以win 10 为例, 找到文件C:\Users\xxzx\AppData\Roaming\Sublime Text 2\Packages\Python\Python.sublime-build 添加 ...

  9. Android基础知识点-Manifest清单文件

    每个应用的根目录中都必须包含一个 AndroidManifest.xml 文件(且文件名精确无误). 清单文件向 Android 系统提供应用的必要信息,系统必须具有这些信息方可运行应用的任何代码. ...

  10. mysql进阶(二十九)常用函数

    mysql进阶(二十九)常用函数 一.数学函数 ABS(x) 返回x的绝对值 BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制) CEILING(x) 返回大于x的最小整数值 EXP ...