• [Leetcode] Partition List


    Partition List 题解

    题目来源:https://leetcode.com/problems/partition-list/description/


    Description

    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.

    Example

    Given 1->4->3->2->5->2 and x = 3,
    return 1->2->2->4->3->5.

    Solution

    
    class Solution {
    public:
        ListNode* partition(ListNode* head, int x) {
            if (head == NULL || head -> next == NULL)
                return head;
            ListNode *lowHead = NULL, *highHead = NULL;
            ListNode *lowTail = NULL, *highTail = NULL;
    
            // 找到最靠前的小于x的链表段头尾和大于x的链表段头尾
            if (head -> val < x)
                lowHead = lowTail = head;
            else
                highHead = highTail = head;
            if (lowHead) {
                while (lowTail -> next) {
                    if (lowTail -> next -> val < x) {
                        lowTail = lowTail -> next;
                    } else {
                        highHead = highTail = lowTail -> next;
                        break;
                    }
                }
                if (!highHead)
                    return head;
            } else {
                while (highTail -> next) {
                    if (highTail -> next -> val >= x) {
                        highTail = highTail -> next;
                    } else {
                        lowHead = lowTail = highTail -> next;
                        break;
                    }
                }
                if (!lowHead)
                    return head;
            }
    
            if (highHead == head) {
                // 将小于x的链表段放到大于x的链表段前面
                highTail -> next = lowTail -> next;
                lowTail -> next = highHead;
            }
    
            // 每次向后探测一个节点,如果大于x则不移动,如果小于x就加到小于x的链表段尾部,
            // 直到读取完整个链表
            while (highTail -> next) {
                if (highTail -> next -> val >= x) {
                    highTail = highTail -> next;
                } else {
                    lowTail -> next = highTail -> next;
                    lowTail = lowTail -> next;
                    highTail -> next = lowTail -> next;
                    lowTail -> next = highHead;
                }
            }
            return lowHead;
        }
    };
    
    

    解题描述

    这道题题意是给出一个链表和一个数x,要求将链表中所有大于或等于x的节点放到小于x的节点后面,前变换位置前后,高低两个链表段内节点的顺序和原链表相同。上面是我自己AC的解法,算法已经写在注释中。虽然这个解法可以在一趟之内完成,但是相对比较繁琐,下面给出的是评论区的高票解法:

    
    class Solution {
    public:
        ListNode* partition(ListNode* head, int x) {
            if (head == NULL || head -> next == NULL)
                return head;
            ListNode *lowHead = new ListNode(0);
            ListNode *highHead = new ListNode(0);
            ListNode *lowTail = lowHead, *highTail = highHead;
            while (head) {
                if (head -> val < x) {
                    lowTail -> next = head;
                    lowTail = lowTail -> next;
                } else {
                    highTail -> next = head;
                    highTail = highTail -> next;
                }
                head = head -> next;
            }
            highTail -> next = NULL;
            lowTail -> next = highHead -> next;
            head = lowHead -> next;
            delete lowHead;
            delete highHead;
            return head;
        }
    };
    
    
    

    其采用的办法是对原来的链表构建2个新的链表lowhigh,然后从前向后扫描链表,每次根据节点元素大小分别放到两个新链表尾部,最后再将high接到low后面即可。

  • 相关阅读:
    原笔迹手写实现平滑和笔锋效果之:笔迹的平滑(二)
    区块链入门(4)Truffle创建项目,编译,及项目部署
    github的基本使用
    ES6新特性之傻瓜式说明
    webpack之傻瓜式教程及前端自动化入门
    查找树莓派ip地址的常用方法
    树莓派的无屏幕安装的教程
    采用QHD分辨率使用kinect2_calibration,完成QHD图像校正
    编译ORBSLAM2 build_ros.sh,实现kinect2在ROS环境下运行ORBSLAM2
    重写kinect2_viewer,编译高博kinect2在orbslam2上跑的程序(解决cmakefile中库依赖和头文件的问题)
  • 原文地址:https://www.cnblogs.com/yanhewu/p/8384579.html
Copyright © 2020-2023  润新知