代码随想录刷题随记4-链表

代码随想录刷题随记4

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

leetcode 链接
没什么技巧,主要考察链表操作的熟练程度

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
    if(head==nullptr){
            return nullptr;
    }
    ListNode * virhead=new ListNode(0);
    virhead->next=head;
    ListNode * cur=head;
    ListNode * next=cur->next;
    ListNode * tmp;
    ListNode * pre=virhead;
    while(cur!=nullptr&&next!=nullptr){
     tmp=next->next;
     pre->next=next;
     cur->next=tmp;
     next->next=cur;
     pre=cur;
     cur=tmp;
     if(cur!=nullptr){
        next=cur->next;
     }   
    }
    return virhead->next;
    }
};

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

leetcode链接
因为是倒数的第n个节点而不是正数的节点,所以为了满足一遍遍历就能找到,可以让快指针和慢指针之间间隔n。快慢指针的经典应用
解题代码如下所示:

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode * virhead=new ListNode(0);
    virhead->next=head;
    ListNode * fast;
    ListNode * slow;
    fast=virhead;
    slow =virhead;
    if(head==nullptr){
        return nullptr;
    }
    while(n>0){
        if(fast==nullptr){
            return head;
        }
        fast=fast->next;
      n--;
    }
    fast=fast->next;//为了让slow指向要删除的上一个,fast多走一部
    while(fast!=nullptr){
        fast=fast->next;
        slow=slow->next;
    }
    slow->next=slow->next->next;
    return virhead->next;
    }
};

面试题 02.07. 链表相交

leetcode链接
可以用集合筛选相同的节点即相交节点,但是那样会有额外的空间
采用双指针的方式解决。
首先先求两个链表的长度,找出长链表比短链表长多少,如果两个链表相交,交点一定在这个长度之后。
题解链接
解题代码:

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode * cur1;
        ListNode * cur2;
        cur1=headA;
        cur2=headB;
        int lengthA=0;
        int lengthB=0;
        while(cur1!=nullptr){
          cur1=cur1->next;
          lengthA++;
        }
        while(cur2!=nullptr){
          cur2=cur2->next;
          lengthB++;
        }
        if(lengthA==0||lengthB==0)
        return nullptr;
        int dif=abs(lengthA-lengthB);
        lengthA>=lengthB? cur1=headA:cur1=headB;
        lengthA>=lengthB? cur2=headB:cur2=headA;
        while(dif>0){
            cur1=cur1->next;
            dif--;
        }
        while(cur1!=nullptr&&cur2!=nullptr){
            if(cur1==cur2){
                return cur1;
            }
            cur1=cur1->next;
            cur2=cur2->next;
        }
         return nullptr;
    }
};

142.环形链表II

leetcode题目链接
主要是如何找到入环节点的那部分的数学推导需要仔细思考一下
解题思路
解题代码
要注意fast边界条件的判断是fast和fast->next都不为空

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if(head==nullptr){
            return nullptr;
        }
        ListNode * fast=head;
        ListNode * slow=head;
        while(fast!=nullptr&&fast->next!=nullptr){
            fast=fast->next->next;
            slow=slow->next;
            if(fast==slow){
                break;
            }
        }
        if(fast==nullptr||fast->next==nullptr){
          return nullptr;
        }
        fast=head;
        while(fast!=slow){
            fast=fast->next;
            slow=slow->next;
        }
        return fast;
    }
};
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值