代码随想录训练营 Day 04:LeetCode 206 19 160

代码随想录训练营 Day 04:LeetCode 206 19 160

206. 反转链表

题目

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

解题

code

双指针法
/**
 * 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* reverseList(ListNode* head) {
        ListNode *pre = nullptr, *cur = head, *tmp;
        while(cur != NULL)
        {
            tmp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
};
递归法
/**
 * 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* reverse(ListNode *pre, ListNode *cur)
    {
        if(cur == NULL) return pre;
        ListNode *tmp = cur->next;
        cur->next = pre;        
        return reverse(cur,tmp);
    }
    ListNode* reverseList(ListNode* head) {

        return reverse(NULL,head);
    }
};

总结

在对链表的结构理解透彻后,这个问题很好解决;
写代码的时候遇到的一点小问题:在连续定义多个指针变量时,注意每个变量前都要有*号

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

题目

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

code

/**
 * 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 *start = new ListNode(), *end = start, *_dummyhead = start;
        start->next = head;
        int i = 0;
        while(i < n)
        {
            end = end->next;
            i++;
        }
        while(end->next != nullptr)
        {
            start = start->next;
            end = end->next;
        }
        ListNode *tmp = start->next;
        start->next = tmp->next;
        delete tmp;
        tmp = nullptr;
        
        return _dummyhead->next;
    }
};

总结

链表中,可以多个变量指向同一个结点(地址),需要注意的是,任意一个变量发生变化,都会对链表产生影响,,尤其是在修改当前节点的下一个节点指向时,一定要提前保存好本指向的那个节点,不然会彻底丢失该节点信息,再难寻觅。

160. 相交链表

题目

在这里插入图片描述

解题

这道题的主要难点在于理解相交这一概念,是指两个链表在从某一节点开始,之后的节点完全一致(结点的地址、链接顺序均完全相同),而不只是简单的数值相等。

code

/**
 * 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) {
        ListNode *curA = new ListNode(), *curB = new ListNode(), *tmp = curA;
        int i;

        //获取两个链表长度
        curA->next = headA;
        int lenA = 0;
        while(curA->next != NULL){
            lenA ++;
            curA = curA->next;
        }
        curB->next = headB;
        int lenB = 0;
        while(curB->next != NULL){
            lenB ++;
            curB = curB->next;
        }

        curA = headA;
        curB = headB;

        // 对比两个链表长度
        // 不等
        if(lenA > lenB)
        {
            for(i = 0; i < (lenA - lenB); i++)
                curA = curA->next;

        }
        else if(lenA < lenB)
        {
             for(i = 0; i < (lenB - lenA); i++)
                curB = curB->next;
        }

        // 后移,对比结点是否相同
        for(i = 0; i < min(lenA,lenB); i++ ){
            if (curA == curB)
                break;
            else{
                curA = curA->next;
                curB = curB->next;
            }
        }
    return curA;
    }

};

142. 环形链表 II

题目

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

code

总结

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值