代码随想录第四天 204两两交换 19删除链表的倒数第N个结点面试题 02.07 链表相交 142环形链表 II

本文介绍了链表中的四种常见操作:交换相邻节点、删除倒数第N个节点、寻找链表相交点以及检测环形链表。重点强调了快慢指针的使用和对空指针异常的注意。
摘要由CSDN通过智能技术生成

一 204两两交换

对于链表而言,要对某个结点进行操作,往往要站在该结点的前一位或者几位,因为链表往后找是容易的,往前找是费劲的。本题要注意模拟交换操作的进行。

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dummyhead = new ListNode(0);
        dummyhead->next = head;
        ListNode* cur = dummyhead;
        while(cur->next != nullptr && cur->next->next!=nullptr){
            ListNode* temp = cur->next;
            ListNode* temp2 = cur->next->next->next;

            cur->next = cur->next->next;
            cur->next->next = temp;
            cur->next->next->next = temp2;

            cur = cur->next->next;
        }
        return dummyhead->next;
    }
};

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

两个指针相距固定的距离,然后一起向后移动

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummyhead = new ListNode(0);
        dummyhead->next = head;
        ListNode* fast = dummyhead;
        ListNode* slow = dummyhead;
        while(n-- && fast != nullptr){
            fast = fast->next;
        }
        while(fast->next != nullptr){
            fast = fast->next;
            slow = slow->next;
        }
        ListNode* p = slow->next;
        slow->next = p->next;
        delete p;
        
        return dummyhead->next;
    }
};

三 面试题02.07 链表相交

链表相交结点后面的结点也全部相交,因此从链表后向前看是有规律的

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int count1 = 0;
        int count2 = 0;
        ListNode* curA = headA;
        ListNode* curB = headB;
        while(curA != nullptr){
            count1++;
            curA = curA->next;
        }
        while(curB != nullptr){
            count2++;
            curB = curB->next;
        }
        curA = headA;
        curB = headB;
        if (count2 > count1) {
            swap (count1, count2);
            swap (curA, curB);
        }
        
        int count = count1 - count2; 
        
        while(count--){
            curA = curA->next;
        }
        while(curA != curB && curA != nullptr){
            curA = curA->next;
            curB = curB->next;
        }
        return curA;
    }
};

四 142环形链表 II

快慢指针,需要进行画图模拟计算,如果存在环,则一次走两步的快指针必定能赶上一次走一步的慢指针,且慢指针在环中不可能走完完整的一圈。

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode* fast = head;
        ListNode* slow = head;
        while(fast != nullptr && fast->next != nullptr){
            slow = slow->next;
            fast = fast->next->next;
            if(slow == fast){
                ListNode* index1 = fast;
                ListNode* index2 = head;
                while (index1 != index2) {
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index2; 
            }
        }
        return NULL;
    }
};

五 总结

  1. 链表中双指针,快慢指针的运用非常常见,要把握好。
  2. 链表中对于空指针的访问异常是非常典型的问题,要注意到。
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值