代码随想录算法训练营第四天|24. 两两交换链表中的节点,19. 删除链表的倒数第 N 个结点,面试题 02.07. 链表相交,142. 环形链表 II。

代码随想录算法训练营第四天|24. 两两交换链表中的节点,19. 删除链表的倒数第 N 个结点,面试题 02.07. 链表相交,142. 环形链表 II。

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

题目链接:24. 两两交换链表中的节点,难度:简单

【实现代码】

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* cur = head;
        ListNode* pre;
        ListNode* ppre;
        ListNode* tmp;
        for (int i = 0; cur != nullptr; i++) {            
            tmp = cur->next;
            if (i % 2 != 0) {
                if (i > 2) {
                    ppre->next = pre->next;
                }             
                pre->next = cur->next;                
                cur->next = pre;                
                ppre = pre;
                
            }
            pre = cur;
            if (i == 1) {
                head = cur;
            }
            cur = tmp;
        }
        return head;
    }
};

【解题思路】

我采用了四个指针来解决问题,方法比较笨了点。

  1. 找到奇数的index,然后将其和其前继节点反转
  2. 记录该奇数节点,用来指向下一个反转的子链
  3. 有大佬实现了递归写法,代码简洁,原理清楚,坐飞机去看代码

【总结】

【存在的问题】每次思考的很久,可能是不专心,效率低
【收获】对指针和链表有了更深的理解。

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

题目链接: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) {
        ListNode* dummyHead = new ListNode(0, head);
        ListNode* fast = dummyHead;
        ListNode* slow = dummyHead;
        n++;
        while (n-- && fast != nullptr) {
            fast = fast->next;
        }
        while (fast != nullptr) {
            fast = fast->next;
            slow = slow->next;
        }
        ListNode* tmp = slow->next;
        slow->next = slow->next->next;
        delete tmp;
        return dummyHead->next;
    }
};

【解题思路】

双指针解法,很巧妙。

  1. fast指针指向第n+1个节点(如果是fast->next==nullptr是结束条件,fast指向第n个就可以)
  2. fast和slow同时移动,结束条件是fast!=nullptr
  3. 此时slow指向的下一个节点为删除节点(删除链表节点时,应指向被删除节点的前一个节点)
  4. 为方便操作前节点,引入虚拟头节点。
  5. 原理:当fast为nullptr时,slow和fast之间的节点数就是n。

【总结】

【存在的问题】当看到一趟扫描时,考虑到了使用双指针实现,但是没有想到具体怎么做。

面试题 02.07. 链表相交

题目链接:面试题 02.07. 链表相交,难度:简单

【实现代码】

/**
 * 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 aSize = 0;
        int bSize = 0;
        ListNode* curA = headA;
        ListNode* curB = headB;
        while (curA != nullptr) {
            aSize++;
            curA = curA->next;
        }
        while (curB != nullptr) {
            bSize++;
            curB = curB->next;
        }
        curA = headA;
        curB = headB;
        if (aSize > bSize) {
            for (int i = 0; i < aSize - bSize; i++) {
                curA = curA->next;
            }
        } else {            
            for (int i = 0; i < bSize - aSize; i++) {
                curB = curB->next;
            }
        }
        while (curA != curB) {
            curA = curA->next;
            curB = curB->next;
        }
        return curA;
    }
};

【解题思路】

两个链表相交后的节点数量是相同的,所以当两个链表对齐后,可以同时移动判断指针是否相等。当两个链表没有相交点时,最后都指向nullptr,所以不用单独判断。

142. 环形链表 II

题目链接:142. 环形链表 II,难度:中等

【实现代码】

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

【解题思路】

还是卡哥讲的比较好卡哥的讲解视频

【总结】

【收获】掌握了如何判断环的方法以及如何找到环入口。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值