LeetCode 24. 两两交换链表中的节点 、19.删除链表的倒数第N个节点 、面试题 02.07. 链表相交 、142.环形链表II

24. 两两交换链表中的节点 - 力扣(LeetCode)

/**
 * 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==nullptr||head->next==nullptr){
            return head;
        }
        ListNode* dummy=new ListNode(0);
        dummy->next=head;
        ListNode* prev=dummy;
        ListNode* cur=head;
        while(cur!=nullptr&&cur->next!=nullptr){
            ListNode* next=cur->next;
            cur->next=next->next;
            next->next=cur;
            prev->next=next;
            prev=cur;
            cur=cur->next;
        }
        return dummy->next;
    }
};

19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

/**
 * 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* tmp=head;
        int count=0;
        while(tmp!=nullptr){
            count++;
            tmp=tmp->next;
        }
    
        ListNode* dummy=new ListNode(0);
        dummy->next=head;
        if(count==1){
            head=nullptr;
            return head;
        }else{
            ListNode* cur=dummy;
            for(int i=1;i<=count-n;i++){
                cur=cur->next;
            }
            cur->next=cur->next->next;
            ListNode* ans=dummy->next;
            delete dummy;
            return ans;
        }
       
    }
};

面试题 02.07. 链表相交 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    int getCount(ListNode* L){
        int count=0;
        ListNode* current = L; //使用 current 指针迭代链表
        while(current!=nullptr){
            count++;
            current=current->next;
        }
        return count;
    }
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if(headA==NULL||headB==NULL){
            return nullptr;
        }
        ListNode* pA=headA;
        ListNode* pB=headB;
        int countA = getCount(pA);
        int countB = getCount(pB);

        pA=headA;
        pB=headB;

        // 先将较长的链表指针移动到与较短链表相同长度的位置
        if(countA<=countB){
           for(int i=0;i<countB-countA;i++){
              pB=pB->next;
           }
        }else{
           for(int i=0;i<countA-countB;i++){
              pA=pA->next;
           }
        }

        // 遍历链表,找到相交的节点
        while(pA!=nullptr && pB!=nullptr && pA!=pB){
            pA = pA->next;
            pB = pB->next;
        }
       
        return pA; // 返回相交的节点,即使没有相交也会返回 null
    }
};

142. 环形链表 II - 力扣(LeetCode)

/**
 * 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!=nullptr){
            slow=slow->next;
            if(fast->next==nullptr){
                return nullptr;
            }
            fast=fast->next->next;
            if(fast==slow){
                ListNode* tmp=head;
                //第二次相遇
                while(tmp!=slow){
                    tmp=tmp->next;
                    slow=slow->next;
                }
                return tmp;
            }
        }
        return NULL;
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值