【leetcode】86 partition list

题目说明

https://leetcode-cn.com/problems/partition-list/description/

给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。

你应当保留两个分区中每个节点的初始相对位置。

解法1

遍历链表,记录最后一个小于x的结点less,当遍历到的结点小于x时,将该点插入到less之后,直到遍历结束。

/*
 * 时间复杂度:O(n)
 * 记录最后一低点小于x的点less
 * 遍历链表,当遍历到的点(pre)的下一个点(cur)小于x时,将下一点(cur)插入到less之后,less相应向后移动一位
 * 并且pre需要指向原cur的下一个点(next),保证链表不会断,所以要以pre来遍历
 * 特殊情况:当满足条件时,当less与当前点前一点重合时,只需要向后移动一位即可
 */
ListNode* partition(ListNode* head, int x) {
    if (head == NULL)
        return NULL;
    ListNode *dummy = new ListNode(0);
    dummy->next = head;

    ListNode *pre = dummy;
    ListNode *less = dummy;
    ListNode *cur = NULL;
    ListNode *cur_next = NULL;

    while(pre->next){
        cur = pre->next;
        if (cur->val < x){
            if (pre == less){
                less = less->next;
                pre = pre->next;
                continue;
            }
            cur_next = cur->next;//记录当前点下一个结点
            cur->next = less->next;//当前点指向less的下一个结点
            less->next = cur;//less指向当前点,即完成插入操作
            pre->next = cur_next;//前一点指向所记录的原当前点下一个结点,保持链表完整性
            less = less->next;//less后移,pre不需要后移,因为其next已经变化了
        } else
            pre = pre->next;
    }
    return dummy->next;
}

解法2

添加两个链表,分别记录小于x的结点与大于等于x的结点,遍历链表时,将对应的结点插入到链表中,最后将两个链表拼接到一起。

/*
 * 时间复杂度:O(n)
 * 添加两个链表
 * 小于x的记录到一个链表
 * 其他大于等于的记录到另一外链表
 * 然后两个链表拼接
 * 注意:最后一个结点需要指向NULL
 */
ListNode* partition2(ListNode* head, int x) {
    ListNode *lessNode = new ListNode(0);
    ListNode *moreNode = new ListNode(0);

    ListNode *lessCur = lessNode;
    ListNode *moreCur = moreNode;

    ListNode *cur = head;
    while(cur){
        if (cur->val < x){
            lessCur->next = cur;
            lessCur = lessCur->next;
        }else{
            moreCur->next = cur;
            moreCur = moreCur->next;
        }
        cur = cur->next;
    }

    lessCur->next = moreNode->next;
    moreCur->next = NULL;
    return lessNode->next;
}

原文地址:https://www.cnblogs.com/JesseTsou/p/9569942.html

时间: 2024-08-28 04:24:05

【leetcode】86 partition list的相关文章

【一天一道LeetCode】#86. Partition List

一天一道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 origin

【leetcode】1043. Partition Array for Maximum Sum

题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return the largest sum of the given arr

【LeetCode】Partition List

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

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n