【LeetCode】链表

链表

与数组相似,链表也是一种 线性 数据结构。

  • 了解单链表和双链表的结构
  • 在单链表或双链表中实现遍历、插入和删除
  • 分析在单链表或双链表中的各种操作的复杂度
  • 在链表中使用双指针技巧(快指针慢指针技巧)
  • 解决一些经典问题,例如反转链表
  • 分析你设计的算法的复杂度
  • 积累设计和调试的经验
单链表
1、设计链表

在这里插入图片描述
在这里插入图片描述

代码:

class MyLinkedList {
private:
    struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x,ListNode* n) : val(x), next(n) {}
};
public:
    /** Initialize your data structure here. */
    ListNode *head;
    int size;
    
    MyLinkedList() {
        head=NULL;
        size=0;
    }
    
    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    int get(int index) {
        if(index<0||index>=size) return -1;
        ListNode *cur=head;
        for(int i=0;i<index;i++)
        {
            cur=cur->next;
        }
        return cur->val;
    }
    
    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    void addAtHead(int val) {
        ListNode *tmp=new ListNode(val,head);
        head=tmp;
        size++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        ListNode *cur=head;
        while(cur->next) cur=cur->next;
        cur->next=new ListNode(val,NULL);
        size++;
    }
    
    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    void addAtIndex(int index, int val) {
        if(index<=0) 
        {
            addAtHead(val);
            return;
        }
        if(index>size) return;
        
        ListNode *cur=head;
        for(int i=0;i<index-1;i++)
        {
            cur=cur->next;
        }
        ListNode *tmp=new ListNode(val,cur->next);
        cur->next=tmp;
        size++;
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        if(index<0||index>=size) return;
        if(index==0)
        {
            head=head->next;
            size--;
            return;
        }
        ListNode *cur=head;
        for(int i=0;i<index-1;i++)
        {
            cur=cur->next;
        }
        cur->next=cur->next->next;
        size--;
    }
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */
双指针技巧
1、环形链表

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode *fast=head;
        ListNode *slow=head;
        while(fast && fast->next)
        {
            fast=fast->next->next;
            slow=slow->next;
            if(fast==slow)
                return true;
        }
        return false;
    }
};
2、环形链表Ⅱ

https://leetcode-cn.com/explore/learn/card/linked-list/194/two-pointer-technique/745/
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

/**
 * 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 *fast=head;
        ListNode *slow=head;
        int step=0;
        int cycle=0;
        while(fast && fast->next)
        {
            fast=fast->next->next;
            slow=slow->next;
            step++;
            if(fast==slow)
            {
                cycle=2*step-step;  
                ListNode *ptr=head;
                while(ptr!=slow)
                {
                    ptr=ptr->next;
                    slow=slow->next;
                }
                return ptr;
            }
        }
        return NULL;
    }
};
3、相交链表

在这里插入图片描述
在这里插入图片描述
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Reference of the node with value = 8
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
在这里插入图片描述
输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Reference of the node with value = 2
输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
在这里插入图片描述
输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
解释:这两个链表不相交,因此返回 null。
在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if(headA==NULL||headB==NULL) return NULL;
        ListNode *str1=headA;
        ListNode *str2=headB;
        int size=1;
        while(str1->next)
        {
            str1=str1->next;
            size++;
        }
        str1=headA;
        for(int i=0;i<size;i++)
        {
            while(str2)
            {
                if(str1==str2) return str1;
                str2=str2->next;
            }
            str2=headB;
            str1=str1->next;
        }
        return NULL;
    }
};
4、删除链表中的倒数第N个节点

在这里插入图片描述
代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* str1=head;
        ListNode* str2=str1;   
        while(str1&&str1->next)
        {
            str2=str1;
            for(int i=0;i<n;i++)
            {
                str2=str2->next;
            }
            if(str2==NULL)
            {
                head=str1->next;
                return head;
            }
            else if(str2->next==NULL)
            {
                str1->next=str1->next->next;
                str1=head;
                return str1;
            }
            else
            {
                str1=str1->next;
            }
        }
        return NULL;
    }
};
总结
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值