笔面试算法--链表专题1(链表中倒数第k个结点、删除链表的倒数第n个节点、反转链表、链表内指定区间反转、判断链表中是否有环、链表中环的入口节点、两个链表的第一个公共结点、合并有序链表)

本文探讨了如何在给定链表中找到第k个节点,并展示了如何高效地实现链表反转操作。涉及的方法包括双指针技巧和节点交换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pHead ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    ListNode* FindKthToTail(ListNode* pHead, int k) {
        // write code here
        ListNode* fast=pHead;
        ListNode* slow=pHead;
        int ct=0;
        while(ct<k){
            if(fast==NULL)
                return NULL;
            fast=fast->next;
            ct++;
        }
        while(fast!=NULL){
            fast=fast->next;
            slow=slow->next;
        }
        return slow;
    }
};

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @param n int整型 
     * @return ListNode类
     */
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        // write code here
        ListNode* fast=head;
        ListNode* slow=head;
        ListNode* ft=new ListNode(-1);
        ft->next=head;
        ListNode* pre=ft;
        int ct=0;
        while(ct<n){
            fast=fast->next;
            ct++;
        }
        while(fast!=NULL){
            fast=fast->next;
            pre=slow;
            slow=slow->next;
        }
        pre->next=slow->next;
        return ft->next;
    }
};

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        ListNode* pre=NULL;
        ListNode* now=pHead;
        ListNode* next=NULL;
        while(now!=NULL){
            next=now->next;
            now->next=pre;
            pre=now;
            now=next;
        }
        pHead=pre;
        return pHead;
    }
};

 

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 
     * @param m int整型 
     * @param n int整型 
     * @return ListNode类
     */
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        // write code here
        ListNode* ft=new ListNode(-1);
        ft->next=head;
        ListNode* Mpre=ft;
        for(int i=0;i<m-1;i++)
            Mpre=Mpre->next;
        ListNode* M=Mpre->next;
        ListNode* pre=M;
        ListNode* now=M->next;
        ListNode* Next=NULL;
        for(int i=m+1;i<=n;i++){
            Next=now->next;
            now->next=pre;
            if(i==n){
                M->next=Next;
                Mpre->next=now;
            }
            pre=now;
            now=Next;
        }
        return ft->next;
        }
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode* fast=head;
        ListNode* slow=head;
        while(slow!=NULL&&fast->next!=NULL&&fast->next->next!=NULL){
            slow=slow->next;
            fast=fast->next;
            fast=fast->next;
            if(fast==slow)
                return true;
        }
        return false;
    }
};

/**
 * 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* fast=head;
        ListNode* slow=head;
        while(slow!=NULL&&fast->next!=NULL&&fast->next->next!=NULL){
            slow=slow->next;
            fast=fast->next;
            fast=fast->next;
            if(fast==slow){
                ListNode* ft=head;
                while(ft!=fast){
                    ft=ft->next;
                    fast=fast->next;
                }
                return ft;
            }
        }
        return NULL;
    }
};

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        ListNode* l1=pHead1;
        ListNode* l2=pHead2;
        int cd1=0;
        int cd2=0;
        while(l1!=NULL||l2!=NULL){
            if(l1!=NULL)
                l1=l1->next;
            else
                cd2++;
            if(l2!=NULL)
                l2=l2->next;
            else
                cd1++;
        }
        l1=pHead1;
        l2=pHead2;
        while(cd1){
            l1=l1->next;
            cd1--;
        }
        while(cd2){
            l2=l2->next;
            cd2--;
        }
        while(l1!=l2){
            l1=l1->next;
            l2=l2->next;
        }
        return l1;
    }
};

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param l1 ListNode类 
     * @param l2 ListNode类 
     * @return ListNode类
     */
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        // write code here
        if(l1==NULL)
        return l2;
        if(l2==NULL)
            return l1;
        ListNode* ans=new ListNode(-1);
        ListNode* now=ans;
        while(l1!=NULL||l2!=NULL){
            if(l2==NULL||(l1!=NULL&&(l1->val)<=(l2->val))){
                now->next=l1;
                l1=l1->next;
            }
            else{
                now->next=l2;
                l2=l2->next;
            }
            now=now->next;
        }
        now->next=NULL;
        return ans->next;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值