2022年10月15日学习打卡

105 阅读3分钟

day4

24. 两两交换链表中的节点

题目链接/文章讲解/视频讲解: programmercarl.com/0024.%E4%B8…

个人思路:

该题采用虚拟头结点,这样会方便很多。在最开始没有采用虚拟头节点,因此操作比较繁琐,但采用带头节点的则操作很简单,在做下面题时,最好画图,严格按照图中的顺序来进行操作。

faeeb1f733b76319950bd5a9f0ddfc7.jpg

代码:

/**
 * 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* swapPairs(ListNode* head) {
        if(!head || (!head->next)) return head;  //当链表中只有一个节点或者链表为空
        ListNode *dummyhead = new ListNode(0,head);         //创建一个头节点
        ListNode* cur = dummyhead;
        while(cur->next&&cur->next->next){
            ListNode *tmp1 = cur->next;
            ListNode *tmp2 = cur->next->next;

            cur->next = tmp2;
            tmp1->next = tmp2->next;
            tmp2->next = tmp1;

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

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

题目链接/文章讲解/视频讲解:programmercarl.com/0019.%E5%88…

**个人思路:**该题较为简单,比较常见,题目要求在一次遍历中完成操作,删除倒数第N个节点相当从正向走M-N步(假设整个链表的长度为M),可以通过先让一个指针先走N步,另外一个指针指向开头,即后面的指针相对于前面的指针距离为N,当后面的指针指向末尾时,前面的指针距离末尾也就为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) {
        ListNode *dummyHead =new ListNode(0,head);
        ListNode *end = dummyHead;
        while(n--)
        {
            end = end->next;
        }
        ListNode *pre = dummyHead;
        while(end->next)   
        {
            pre = pre->next;
            end = end->next;
        }
        ListNode *tmp = pre->next;
        pre->next = tmp->next;
        delete tmp;
        return dummyHead->next;
    }
};

面试题 02.07. 链表相交

题目链接/文章讲解:programmercarl.com/%E9%9D%A2%E…

个人思路: 本题比较基础,与上面的题有类似的思路,首先遍历整个链表,记录下整个链表的长度,如果两个链表相交,那么他们相交的节点距离链表末尾的长度一定是恒定的,因此只需要从前面开始进行寻找,记录下两个链表的差值,让其中一个链表(较长的)先遍历差值的(此时如果两者相交则只需要再同时走一样的长度就可以相遇),再两个同时遍历,如果两个链表有相同的节点,则该节点一定为链表的第一个相交的节点。

代码:

/**
 * 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) {
        int lenA =0;
        int lenB =0;
        ListNode *startA = headA;
        ListNode *startB = headB;
        while(startA)
        {
            lenA++;
            startA = startA->next;
        }
        while(startB)
        {
            lenB++;
            startB = startB->next;
        }
        startA = headA;
        startB = headB;
        if(lenA - lenB >0)
        {
            int dec = lenA - lenB;
            while(dec--)
            {
                startA = startA->next;
            }
            while(lenB--)
            {
                if(startA == startB) return startA;
                startA = startA->next;
                startB = startB->next;
                
            }
            if(startA == startB) return startA;
            else return NULL;
        }
        else{
            int dec = lenB - lenA;
            while(dec--)
            {
                startB = startB->next;
            }
            while(lenA--)
            {
                if(startA == startB) return startA;
                startA = startA->next;
                startB = startB->next;
            }
            if(startA == startB) return startA;
            else return NULL;
        }
    }
};

142.环形链表II

题目链接/文章讲解/视频讲解:programmercarl.com/0142.%E7%8E…

个人思路:

快指针的速度为慢指针的2倍时,如果有环那么两个指针一定会相遇,当两者相遇时一定,在从起点处和相遇点处定义两个指针,以同样为1的速度进行走时,一定会在入口相遇。

代码:

/**
 * 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;
        ListNode *fast = head;
        while(fast&&fast->next)
        {
            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;
    }
};