一天一道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. 《Effective Java》 读书笔记(一)

    对象的创建与销毁 ITEM1 使用静态工厂方法代替构造函数 传统的新建一个对象的方法是通过构造函数: Foo foo =new Foo(); 一个类也可以提供一个静态方法产生一个对象: Boolean ...

  2. monitoring with Prometheus

    Prometheus是一款开源的监控工具,支持k8s metrics的数据格式,同时也支持通过k8s api进行服务发现从而实现对自定义的metrics进行监控.下面通过一个示例来介绍如何将Prome ...

  3. 关于spring定时任务被多次调用的问题

    在项目开发中,难免会用到定时任务,如果你的项目中用了spring这个框架,那么恭喜你,你的定时任务的创建将变得无比简单. 代码中只需要一个 @Scheduled标签,然后配置对应的执行频率即可 pas ...

  4. ACM 饭卡

    Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...

  5. Node.js 字符串解码器

    稳定性: 3 - 稳定 通过 require('string_decoder') ,可以使用这个模块.字符串解码器(StringDecoder)将缓存(buffer)解码为字符串.这是 buffer. ...

  6. mysql5.7在centos上安装的完整教程以及相关的“坑”

    安装前的准备 Step1: 如果你系统已经有mysql,如一般centos自带mysql5.1系列,那么你需要删除它,先检查一下系统是否自带mysql yum list installed | gre ...

  7. Zookeeper命令行操作(常用命令;客户端连接;查看znode路径;创建节点;获取znode数据,查看节点内容,设置节点内容,删除节点;监听znode事件;telnet连接zookeeper)

    8.1.常用命令 启动ZK服务 bin/zkServer.sh start 查看ZK服务状态 bin/zkServer.sh status 停止ZK服务 bin/zkServer.sh stop 重启 ...

  8. SQL Server 虚拟化(1)——虚拟化简介

    本文属于SQL Server虚拟化系列 前言: 现代系统中,虚拟化越来越普遍,如果缺乏对虚拟化工作原理的理解,那么DBA在解决性能问题比如降低资源争用.提高备份还原速度等操作时就会出现盲点.所以基于本 ...

  9. Mac 下 查看 使用某端口的进程和关闭该进程的命令

    查看使用某端口的进程 最简单的命令是: lsof -i :端口号 如果要使用管理员权限那么就是: sudo lsof -i :端口号 所以查看 使用某端口号3000的进程可以使用: lsof -i : ...

  10. An internal error occurred during: "Retrieving archetypes:". GC overhead limit exceeded

    An internal error occurred during: "Retrieving archetypes:".GC overhead limit exceeded 异常, ...