LeetCode日记20200202

本文深入解析LeetCode中多个中等难度的链表题目,包括链表中的下一个更大节点、环形链表2、扁平化多级双向链表等,提供高效的算法思路与代码实现,如栈、双指针技巧和深度优先搜索等,助你掌握链表题目的核心解法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode日记2020.2.2


链表,今天题总体难度较前几天有所增加,以后要回顾总结。

1019 链表中的下一个更大节点(mid)

这道题虽然暴力解法能够通过,但是利用栈一次遍历才是该题真正的解法。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

class Solution {
public:
    vector<int> nextLargerNodes(ListNode* head) {
       stack<pair<int,int>> s;
       vector<int> res;
       int cnt = 0;
       while(head!=NULL)
       {
           res.push_back(0);
           int val = head->val;
           while(!s.empty() && s.top().second<val)
           {
               res[s.top().first] = val;
               s.pop();   
           }
           s.push(pair<int, int>(cnt, val));
           ++cnt;
           head=head->next;
       }
       return res;
    }

};

142 环形链表2(mid)

比官方题解好懂
https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/shuang-zhi-zhen-qing-xi-ti-jie-zhen-zheng-cong-shu/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *slow = head, *fast = head;
        do{
            if(fast==NULL||fast->next==NULL)
                return NULL;
            
            fast = fast->next->next;
            slow = slow->next;
        }while(slow!=fast);

        slow=head;
        while(slow!=fast)
        {
            slow=slow->next;
            fast=fast->next;
        }

        return slow;
    }
};

430 扁平化多级双向链表(mid)

一个变形的深度优先搜索,用栈实现。
注意子链表头中的prev为空并没有指向父节点,有点坑。

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* prev;
    Node* next;
    Node* child;
};
*/
class Solution {
public:
    Node* flatten(Node* head) {
       if(head==NULL)
           return head;

       stack<Node*> s;
       s.push(head);
       while(!s.empty())
       {
            auto p = s.top();
            while(p->child==NULL&&p->next!=NULL)
                p=p->next;

            if(p->child!=NULL)
            {
                s.top()=p;
                s.push(p->child);
            }
            else
            {
                s.pop();
                if(!s.empty())
                {
                    auto temp = s.top()->next;
                    s.top()->next = s.top()->child;
                    s.top()->child = NULL;
                    s.top()->next->prev=s.top();
                    p->next = temp;
                    s.top() = p;
                    if(temp!=NULL)
                        temp->prev = p;
                }
            }
       }
       return head;
    } 
  
};

138 复制带随即指针的链表(mid)

可以用复制图的算法,但官方题解的方法很巧妙。

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/
class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(head==NULL)
            return head;

        auto myhead = head;
        while(head!=NULL)
        {
            Node* node = new Node(head->val);
            auto temp = head->next;
            head->next = node;
            node->next = temp;
            node->random = head->random;
            head=head->next->next;
        }

        Node* dummyH = new Node(-1);
        Node* newEnd = dummyH;
        head = myhead;
        while(head!=NULL)
        {
            if(head->next->random!=NULL)
                head->next->random = head->next->random->next;

            head = head->next->next;
        }
        head = myhead;
        while(head!=NULL)
        {
            newEnd->next = head->next;
            head->next = head->next->next;
            head = head->next;
            newEnd = newEnd->next;
        }
        newEnd->next = NULL;
        return dummyH->next;
    }
};

61 旋转链表(mid)

链表储存结构不同于数组,因此不要让处理数组的思维局限住。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(head==NULL || k==0)
            return head;

        auto End = head;
        int cnt = 1;
        while(End->next!=NULL)
        {
            End=End->next;
            ++cnt;
        }
        End->next = head;
        k %= cnt;
        k = cnt - k;
        cnt = 1;
        while(cnt<k)
        {
            head=head->next;
            ++cnt;
        }
        auto temp = head;
        head=head->next;
        temp->next = NULL;
        return head;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值