代码随想录day4 leetcode24,19,02.07面试题,142,138

感觉链表最难的就是处理越界问题

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

class Solution {
public:
    //链表题很容易出现段错误,一定要注意判断是否为空
    ListNode* swapPairs(ListNode* head) {
        ListNode* dum = new ListNode(0);
        dum->next = head;
        ListNode* cur = dum;

        while(cur->next != nullptr && cur->next->next != nullptr){
            ListNode* tem = cur->next;
            ListNode* tem1 = cur->next->next->next;
            cur->next = cur->next->next;
            cur->next->next = tem;
            cur->next->next->next = tem1;
            cur = tem;
        }
        return dum->next;
    }
};

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

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dum = new ListNode(0);
        dum->next = head;
        ListNode* fast = dum;
        ListNode* slow = dum;
        n++;//让慢指针停在被删除节点的前一个节点
        while(n--){
            fast = fast->next;
        }
        while(fast){
            fast = fast->next;
            slow = slow->next;
        }
        slow->next = slow->next->next;
        return dum->next;
    }
};

面试题 02.07. 链表相交

class Solution {
public:
    int calLenth(ListNode *head){
        int res = 0;
        while(head){
            head = head->next;
            res++;
        }
        return res;
    }
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* heA = headA;
        ListNode* heB = headB;
       //函数返回结果后,链表必须保持其原始结构,所以不能直接使用headA和headB
            int a = calLenth(heA);
            int b = calLenth(heB);
            if(a > b){
                int c= a - b;
                heA = headA;
                heB = headB;
                while(c--){
                    heA = heA->next;
                }   
            }else{
                int c = b - a;
                heA = headA;
                heB = headB;
                while(c--){
                    heB = heB->next;
                }
            }
            while(heA != heB){//两个节点相等时退出循环
                heA = heA->next;
                heB = heB->next;
                if(heA == nullptr || heB == nullptr) return nullptr;
                //只要有一个为空就退出循环
            }
            return heA;
    }
    
};

142.环形链表II

    ListNode *detectCycle(ListNode *head) {
        ListNode* slow = head;
        ListNode* fast = slow;
        while(1){
            if(fast == nullptr || fast->next == nullptr) return nullptr;
            slow = slow->next;
            fast = fast->next->next;
            if(slow == fast) break;
            
        }

        slow=head;
        while(slow != fast){
            slow = slow->next;
            fast = fast->next;
        }
        return slow;
    }
};

138. 复制带随机指针的链表

class Solution {
public:
    //思路:先复制A->A1->B->B1->C->C1->null,再拆分A->B->C->null和A1->B1->C1->null
    Node* copyRandomList(Node* head) {
        if(head == nullptr) return nullptr;//没有这行居然只有3/19的通过率
        Node* cur = head;
        while(cur){
            Node* tem = new Node(cur->val);
            tem->next = cur->next;
            cur->next = tem;
            cur = tem->next;
        }
        cur = head;
        while(cur){
            cur->next->random = cur->random == nullptr ? nullptr : cur->random->next;
            cur = cur->next->next;
        }

        cur = head;
        Node* curNew = cur->next;
        Node* res = cur->next;
        while(cur){//只要cur不为空,curNew就不为空
            cur->next = cur->next->next;
            curNew->next=cur->next == nullptr ? nullptr : cur->next->next;
            cur = cur->next;
            curNew = curNew->next;
        }
        return res;

        
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值