160/19

文章探讨了环形链表的检测、链表中倒数第N个节点的删除以及两个链表的相交节点查找。通过哈希表实现链表遍历,双指针技巧在不同场景中高效解决问题。
摘要由CSDN通过智能技术生成

环形链表:

哈希表 count, insert

/**
 * 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) {
        //通过哈希表遍历所有节点,存储节点
        unordered_set<ListNode*> seen;
        while(head!=nullptr){
            if(seen.count(head)){
                return true;
            }
            seen.insert(head);
            head=head->next;
        }
        return false;
        
    }
};
/*
struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x):val(x),next(NULL){}
};*/

双指针:

/**
 * 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) {
        //需要先判断是否存在链表
    if (head == nullptr || head->next == nullptr) {
        return false;
    }
      //快慢指针
      ListNode* fast =head;
      ListNode* slow = head;
      //更新指针
      while(fast!=NULL&&fast->next!=NULL)
      {
        slow = slow->next;
        fast = fast->next->next;
        if(slow == fast){
            return true;
        }
    }

      return false;
    }

};
/*
struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x):val(x),next(NULL){}
};*/

环形指针2:

/**
 * 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) {
        //判断存在否
        if(head==nullptr||head->next==nullptr){
            return nullptr;
        }
        //快慢指针;
        ListNode* fast=head;
        ListNode* slow=head;
        while(fast!=NULL&&fast->next!=NULL){
            //更替指针
            slow = slow->next;
            fast = fast->next->next;
            if(slow==fast){
                //记录交点 index2 : x=(n-1)(y+z)+z   
                ListNode* index1 = head;
                ListNode* index2 = fast;
                while(index1!=index2){
                    //链表指针下移
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index1;
            }

        }
        return nullptr;
        
    }
};

19.删除链表的倒数第N个结点

双指针:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        //如果存在n的位置不存在,最好先获取链表的长度
        //快慢指针,使用虚拟头节点,方便删除头节点
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* slow = dummyHead;
        ListNode* fast = dummyHead->next;
        //快指针先走n+1步
        while(n--){
            fast = fast->next;
        }
        //接下来一起走,直到fast到null
        while(fast!=nullptr){
            fast = fast->next; 
            slow = slow->next;
        }
        //删除slow的下一个节点
        ListNode* tmp= slow->next;
        slow->next =slow->next->next;
        delete(tmp);
        tmp = nullptr;
        return dummyHead->next;

    }
};

单调栈:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
    //根据栈「先进后出」的原则,我们弹出栈的第 nnn 个节点就是需要删除的节点
    ListNode* dummy= new ListNode(0,head);
    stack<ListNode*> stk;
    ListNode* cur=dummy;
    while(cur){
        stk.push(cur);
        cur = cur->next;
    }
    //删除了倒数n个节点
    for(int i=0;i<n;++i){
        stk.pop();
    }
    //获得删除节点的前驱
    ListNode* prev = stk.top();
    ListNode* tmp1 = prev->next;
    prev->next = prev->next->next;
    delete tmp1;
    tmp1 = nullptr;
    return dummy->next;
    }
};

160.相交链表

/**
 * 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) {
        //指针指向同一块 curA=curB
        //计算链表的长度
        int lenA=0,lenB=0;
        ListNode* curA= headA;
        ListNode* curB= headB;
        while(curA){
            ++lenA;
            curA=curA->next;
        }
        while(curB){
            ++lenB;
            curB=curB->next;
        }
        //重新回到起点
        curA=headA;
        curB=headB;
        //curA只想最大值
        if(lenA<lenB){
            swap(lenA,lenB);
            swap(curA,curB);
        }

 // 求长度差
        int gap = lenA - lenB;
        // 让curA和curB在同一起点上(末尾位置对齐)
        while (gap--) {
            curA = curA->next;
        }
        // 遍历curA 和 curB,遇到相同则直接返回
        while (curA != NULL) {
            if (curA == curB) {
                return curA;
            }
            curA = curA->next;
            curB = curB->next;
        }
        return NULL;
    }
};

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值