链表LeetCode总结

LeetCode题目

  • 203.移除链表元素
  • 206.反转链表
  • 160.相交链表
  • 707.设计链表
  • 24.两两交换链表中的节点
  • 19.删除链表的倒数第 N 个结点
  • 142.环形链表 II

简介

  • 链表题目需要注意的点
    • ListNode结构体要会背
    struct ListNode {
        int val;
        ListNode* next;
    
        ListNode(int _val = 0, ListNode* _next = nullptr)
            : val(_val)
            , next(_next)
        {
        }
    };
    // 结尾记得加分号
    
    • 先判断head节点是否为nullptr,再进入下一步
    • 有效性判断:要对cur取next,就要先判断cur是否为nullptr,再取next
    • 善用dummy哨兵节点和pre指针
    • 使用pre,cur,next,next2指代已有节点,使用temp指代新建节点
    • 一轮处理完后,更新指针
    • 基本处理模板
    if (!head)
    	return head;
    
    ListNode dummy = ListNode(-1, head);
    ListNode *pre = &dummy, *cur = head, *next;
    while (cur) {
        next = cur->next;
    
        ...
    
        pre = cur;
        cur = next;
    }
    
    return dummy.next;
    
  • 链表题目常用方法与概念
    • 快慢指针,注意有效性判断
    while (slow && fast && fast->next) {
    	slow = slow->next;
    	fast = fast->next->next;
    	
    	...
    }
    
    • 对撞指针
    • 相交链表,尾部对齐,再逐个检查尾部指针,相同处为相交处
    • 设计链表时,不要单独实现头插和尾插,统一用指定下标插入实现
    • 循环链表,快指针与慢指针重合处,必定为head
    • 有环链表,在快慢指针重合处、head,同时出发,重合点为环的起点

题目讲解

  • 以综合题707和易错题24、142为例
    class MyLinkedList {
    private:
        struct ListNode {
            int val;
            ListNode* next;
    
            ListNode(int _val = 0, ListNode* _next = nullptr)
                : val(_val)
                , next(_next)
            {
            }
        };
    
        ListNode* dummy;
        int64_t size;
    
    public:
        MyLinkedList()
            : dummy(new ListNode(-1, nullptr))
            , size(0)
        {
        }
    
        int get(int index)
        {
            if (index > (size - 1) || index < 0)
                return -1;
    
            ListNode *pre = dummy, *cur = dummy->next, *next;
            auto cur_index = index - index;
            while (cur) {
                next = cur->next;
    
                if (cur_index == index) {
                    return cur->val;
                }
                ++cur_index;
    
                pre = cur;
                cur = next;
            }
    
            return -1;
        }
    
        // 不要单独实现头插和尾插,统一用指定下标插入实现
        void addAtHead(int val) { return addAtIndex(0, val); }
    
        void addAtTail(int val) { return addAtIndex(size, val); }
    
        void addAtIndex(int index, int val)
        {
            if (index > size || index < 0)
                return;
            if (0 == size) {
                dummy->next = new ListNode(val);
                ++size;
                return;
            }
    
            ListNode *pre = dummy, *cur = dummy->next, *next;
            auto cur_index = index - index;
            while (cur) {
                next = cur->next;
    
                if (cur_index == index) {
                    ListNode* temp = new ListNode(val, cur);
                    pre->next = temp;
                    ++size;
                    break;
                }
                ++cur_index;
    
                pre = cur;
                cur = next;
            }
            if (index == size) {
                ListNode* temp = new ListNode(val, cur);
                pre->next = temp;
                ++size;
            }
    
            return;
        }
    
        void deleteAtIndex(int index)
        {
            if (index > (size - 1) || index < 0)
                return;
    
            ListNode *pre = dummy, *cur = dummy->next, *next;
            auto cur_index = index - index;
            while (cur) {
                next = cur->next;
    
                if (cur_index == index) {
                    pre->next = next;
                    delete cur;
                    --size;
                    break;
                }
                ++cur_index;
    
                pre = cur;
                cur = next;
            }
    
            return;
        }
    };
    
    class Solution {
    public:
        ListNode* swapPairs(ListNode* head)
        {
            if (!head)
                return head;
            if (!(head->next))
                return head;
    
            ListNode dummy = ListNode(-1, head);
            ListNode *pre = &dummy, *cur = head, *next, *next2;
            while (cur && cur->next) {
                next = cur->next;
                next2 = next->next;
    
                pre->next = next;
                next->next = cur;
                cur->next = next2;
    
                // 易错点:pre要先更新,并且cur=next2
                pre = cur;
                cur = next2;
            }
    
            return dummy.next;
        }
    };
    
    class Solution {
    public:
        ListNode* detectCycle(ListNode* head)
        {
            if (!head)
                return NULL;
            if (!(head->next))
                return NULL;
    
            ListNode dummy = ListNode(-1, head);
            ListNode *slow = head, *fast = head;
            // 易错点:快慢指针有效性判断
            while (slow && fast && fast->next) {
                slow = slow->next;
                fast = fast->next->next;
    
                if (slow == fast) {
                    while (slow != head) {
                        slow = slow->next;
                        head = head->next;
                    }
                    return slow;
                }
            }
    
            return NULL;
        }
    };
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值