链表相关OJ及方法总结

news2025/7/20 9:45:04

目录​​​​​​​

第一类:改变链接关系 

第二类:快慢指针 


第一类:改变链接关系 

1. 删除链表中等于给定值 val 的所有结点。

(1)原地删除

struct ListNode* removeElements(struct ListNode* head, int val){
    if(head==NULL)
    {
        return head;
    }
    struct ListNode* cur=head,* pre=NULL,* ret;
    while(cur)
    {
        //需要删除cur
        if(cur->val==val)
        {    
            //cur是首节点,需要改变头结点
            if(pre==NULL)
            {
                ret=cur;
                head=cur->next;
                cur=cur->next;
            }
            //直接删除即可
            else
            {
                 ret=cur;
                 pre->next=cur->next;
                 cur=cur->next;
            }
             free(ret);
        }
        //不用删除,
        else
        {
            pre=cur;
            cur=cur->next;
            
        } 
    }
    return head;
}

(2)建立一个新链表,将不用删除的节点依次尾插

if(head==NULL)
    {
        return head;
    }
    struct ListNode* cur=head,* pre=NULL,*newhead=NULL,*newtail=NULL;
    while(cur)
    {
        
        //需要尾插cur
        if(cur->val!=val)
        {   
            
            //cur是首节点,需要改变newhead
            if(!newhead)
            {
                newhead=newtail=cur;
                
            }
            //直接尾插即可
            else
            {
                newtail->next=cur;
                newtail=newtail->next;
            }
        }
        pre=cur;
        cur=cur->next;
    }
    if(newtail)
        newtail->next=NULL;
    return newhead;

(3)在2的基础上使用带头节点链表

struct ListNode* removeElements(struct ListNode* head, int val){
    if(head==NULL)
    {
        return head;
    }
    struct ListNode* cur=head,* pre=NULL;
    struct ListNode* newhead=(struct ListNode* )malloc(sizeof(struct ListNode)),*newtail=newhead;
    while(cur)
    {
        
        //需要尾插cur
        if(cur->val!=val)
        {   
            newtail->next=cur;
            newtail=newtail->next;
        }
        pre=cur;
        cur=cur->next;
    }
    
    newtail->next=NULL;
    return newhead->next;
}

(4)递归

struct ListNode* removeElements(struct ListNode* head, int val){
    if(head==NULL)
    {
        return head;
    }
    if(head->val==val)
    {
        return  removeElements(head->next, val);
    }
    else
    {
        head->next=removeElements(head->next, val);
        return head;
    }
    
}

2. 反转一个单链表。

 (1)原地逆转


struct ListNode* reverseList(struct ListNode* head){
    struct ListNode* ret=NULL,*cur=head,*pre=NULL;
    while(cur)
    {
        ret=cur->next;
        cur->next=pre;
        pre=cur;
        cur=ret;
    }
    return pre;
}

(2)建立一个新链表,将节点依次头插(带头结点链表)

struct ListNode* reverseList(struct ListNode* head){
    struct ListNode* newhead=(struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode*cur=head,*ret;
    newhead->next=NULL;
    while(cur)
    {  
        ret=cur->next;
        cur->next=newhead->next;
        newhead->next=cur;
        cur=ret;
    }
    
    return newhead->next;
}

(3)递归


struct ListNode* reverseList(struct ListNode* head){
    if(head==NULL||head->next==NULL)
    {
        return head;
    }
    else
    {
        struct ListNode* newhead=reverseList(head->next);
        head->next->next=head;
        head->next=NULL;
        return newhead;
    }
}

第二类:快慢指针 

 3. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则 返回第二个中间结点。

(1)快慢指针

struct ListNode* middleNode(struct ListNode* head){
     struct ListNode* slow = head;
     struct  ListNode* fast = head;
        while (fast != NULL && fast->next != NULL) {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;

}

(2).求出size再遍历

struct ListNode* middleNode(struct ListNode* head){
    struct ListNode* cur = head;
    int size=0;
    while (cur) {
        size++;
        cur=cur->next;
    }
    cur = head;
    size/=2;
    while(size--)
    {
        cur=cur->next;
    }
    return cur;
}

(3)先遍历一遍将数据存入数组再处理


struct ListNode* middleNode(struct ListNode* head){
    struct ListNode* cur = head;
    struct ListNode* a[180];
    int i=0;
    while (cur) {
        a[i++]=cur;
        cur=cur->next;
    }
    return a[i/2];
}

4. 输入一个链表,输出该链表中倒数第k个结点。

(1)快慢指针

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
    if(pListHead==NULL||k==0)
        return NULL;
    struct ListNode* fast,*slow;
    fast=slow=pListHead;
    while(k&&fast)
    {
        fast=fast->next;
        k--;
    }
    if(k)
    {
        return NULL;
    }
    while(fast)
    {
        fast=fast->next;
        slow=slow->next;
    }
    return slow;
}

(2).求出size再遍历size-k次

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
    struct ListNode* cur = pListHead;
    int size=0;
    while (cur) {
        size++;
        cur=cur->next;
    }
    cur = pListHead;
    if(k>size)
        return NULL;
    size-=k;
    while(size--)
    {
        cur=cur->next;
    }
    return cur;
}

5. 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有 结点组成的。

 (1)直接迭代改变链接关系(使用带头链表)

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    struct ListNode* newhead=(struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* tail;
    if(newhead!=NULL)
    {
        tail=newhead;
       
    }
    struct ListNode* p1=list1,*p2=list2;
    tail->next=NULL;
    while(p1&&p2)
    {
        
        if(p1->val<p2->val)
        {
            tail->next=p1;
            tail=tail->next;
            p1=p1->next;
        }
        else
        {
            tail->next=p2;
            tail=tail->next;
            p2=p2->next;
        }
    }

    if(p1)
    {
        tail->next=p1;
    }

    if(p2)
    {
        tail->next=p2;
    }

    return newhead->next;
}

(2)递归

​
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    if(list1==NULL)
    {
        return list2;
    }
    if(list2==NULL)
    {
        return list1;
    }
    if(list1->val<list2->val)
    {
        list1->next=mergeTwoLists(list1->next,list2);
        return list1;
    }
    else
    {
        list2->next=mergeTwoLists(list2->next,list1);
        return list2;
    }
    
    return NULL;

}

​

6. 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结 点之前 。

创建两个链表,分别链接小于x的节点和大于x的节点: 

ListNode* partition(ListNode* pHead, int x) {
        ListNode* newhead1=(ListNode*)malloc(sizeof(ListNode));
        ListNode* newhead2=(ListNode*)malloc(sizeof(ListNode));
        newhead1->next=NULL;
        newhead2->next=NULL;
        ListNode* tail1=newhead1;
        ListNode* tail2=newhead2;
        ListNode* cur=pHead;
        while(cur)
        {
            if(cur->val < x)
            {
                tail1->next=cur;
                tail1=tail1->next;
            }
            else
            {
                tail2->next=cur;
                tail2=tail2->next;
            }
            cur=cur->next;        
         }
       
        tail1->next=newhead2->next;
        tail2->next=NULL;
        return newhead1->next;
    }

7. 链表的回文结构。

 快慢指针加逆序链表

 struct ListNode* reverseList(struct ListNode* head){
        struct ListNode* ret=NULL,*cur=head,*pre=NULL;
        while(cur)
        {
            ret=cur->next;
            cur->next=pre;
            pre=cur;
            cur=ret;
        }
        return pre;
    }

    bool chkPalindrome(ListNode* A) {
        ListNode* slow=A,*fast=A;
        while(fast&&fast->next)
        {
            fast=fast->next->next;
            slow=slow->next;
        }
       
        slow=reverseList(slow);
        
        while(slow&&A!=slow)
        {
            if(slow->val!=A->val)
            {
                return false;
            }
            slow=slow->next;
            A=A->next;

        }
        
        return true;
    }

8. 输入两个链表,找出它们的第一个公共结点。

(1)先遍历两个链表,得出长度差,使得长链表先走长度差的步数,然后一起比较。 

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    int difsize=0;
    struct ListNode *p1=headA,*p2=headB;
    struct ListNode*longL=headA,*shortL=headB;
    while(p1||p2)
    {
        if(p1)
        {
            difsize++;
            p1=p1->next;
        }
        if(p2)
        {
             difsize--;
            p2=p2->next;
        }    
    }
    if(difsize<0)
    {
        longL=headB,shortL=headA;
    }
    difsize=abs(difsize);
    while(difsize--)
    {
        longL=longL->next;
    }
    while(longL)
    {
        if(longL==shortL)
            return longL;
        else
        {
            longL=longL->next;
            shortL=shortL->next;
        }
    }

    return NULL;
}

(2)不记录长度一直遍历达到两个指针指向长度一样的目的。

创建两个指针 初始时分别指向两个链表的头节点 ,然后将两个指针依次遍历两个链表的每个节点。

如果指针 pA不为空,则将指针 pA 移到下一个节点;pB同理。

如果指针 pA为空,则将指针 pA 移到链表 headB的头节点;pB同理。

当指针 pA,pB 指向同一个节点退出。

    if(headA==NULL||headB==NULL)
    {
        return NULL;
    }
    struct ListNode * pA = headA, *pB = headB;
    while (pA != pB) {
        if(!pA)
            pA =headB;
        if(!pB)
            pB =headA;
        if(pA == pB)
            break;
        pA=pA->next,pB=pB->next;
    }
    return pA;
}

(3)哈希表查找

​
struct HashTable {
    struct ListNode *key;
    UT_hash_handle hh;
};

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct HashTable *hashTable = NULL;
    struct ListNode *temp = headA;
    while (temp != NULL) {
        struct HashTable *tmp;
        HASH_FIND(hh, hashTable, &temp, sizeof(struct HashTable *), tmp);
        if (tmp == NULL) {
            tmp = malloc(sizeof(struct HashTable));
            tmp->key = temp;
            HASH_ADD(hh, hashTable, key, sizeof(struct HashTable *), tmp);
        }
        temp = temp->next;
    }
    temp = headB;
    while (temp != NULL) {
        struct HashTable *tmp;
        HASH_FIND(hh, hashTable, &temp, sizeof(struct HashTable *), tmp);
        if (tmp != NULL) {
            return temp;
        }
        temp = temp->next;
    }
    return NULL;
}
​

9. 给定一个链表,判断链表中是否有环。

 (1)快慢指针

bool hasCycle(struct ListNode *head) {
    if(head==NULL)
        return false;
    struct ListNode *fast=head,*slow=head;
    while(fast&&fast->next)
    {
        fast=fast->next->next;
        slow=slow->next;
        if(fast==slow)
            return true;
    }
    
    return false;
    
}

 (2)哈希表

struct hashTable {
    struct ListNode* key;
    UT_hash_handle hh;
};

struct hashTable* hashtable;

struct hashTable* find(struct ListNode* ikey) {
    struct hashTable* tmp;
    HASH_FIND_PTR(hashtable, &ikey, tmp);
    return tmp;
}

void insert(struct ListNode* ikey) {
    struct hashTable* tmp = malloc(sizeof(struct hashTable));
    tmp->key = ikey;
    HASH_ADD_PTR(hashtable, key, tmp);
}

bool hasCycle(struct ListNode* head) {
    hashtable = NULL;
    while (head != NULL) {
        if (find(head) != NULL) {
            return true;
        }
        insert(head);
        head = head->next;
    }
    return false;
}

10. 给定一个链表,返回链表开始入环的第一个结点。 如果链表无环,则返回 NULL 

利用前一题的快慢指针。一指针从相遇点开始走,一指针从头开始,两者一定会在入环点相遇。 

struct ListNode *detectCycle(struct ListNode *head) {
     if(head==NULL)
        return NULL;
    struct ListNode *fast=head,*slow=head;
    while(fast&&fast->next)
    {
        fast=fast->next->next;
        slow=slow->next;
        if(fast==slow)
        {
            slow=head;
            while(slow!=fast)
            {
                fast=fast->next,slow=slow->next;
            }
            return fast;
        }
    }
    return NULL;
}

 11. 给定一个链表,每个结点包含一个额外增加的随机指针,该指针可以指向链表中的任何结点 或空结点。 要求返回这个链表的深度拷贝。

方法一:遍历两遍,第一遍创建新节点将next链接好,同时记录源节点与新节点对应关系,第二遍完成random的链接。

 


struct Node* copyRandomList(struct Node* head) 
{
    if(head==NULL)
        return NULL;
    struct Node* arr1[1001];
    struct Node* arr2[1001];
    struct Node*cur=head;
    struct Node* newguard=(struct Node*)malloc(sizeof(struct Node));
    struct Node*ptail=newguard;
    assert(newguard);
    int index=0;
    while(cur)
    {
        struct Node* ret=(struct Node*)malloc(sizeof(struct Node));
        ret->val=cur->val;
        ptail->next=ret;
        ptail=ret;
        arr1[index]=cur;
        arr2[index]=ret;
        cur=cur->next;
        index++;
    }
    cur=head;
    struct Node*newcur=newguard->next;
    while(cur)
    {
        struct Node* obj=cur->random;
        if(obj!=NULL)
        {
            for(int i=0;i<index;i++)
            {
                if(obj==arr1[i])
                {
                    newcur->random=arr2[i];
                    break;
                }
            }
        }
        else
        {
            newcur->random=NULL;
        }
        cur=cur->next,newcur=newcur->next;
    }
    ptail->next=NULL;
    return newguard->next;
}

方法二:新节点先链接进入原链表然后将新节点的random链接好,此时原来节点random指向的源节点后面就可以直接找到新节点应该指向的位置。

分为三步:

    //创建新节点

    //改变random指向

    //将新节点剥离

 


struct Node* copyRandomList(struct Node* head) 
{
    if(head==NULL)
        return NULL;
    struct Node*cur=head;
    //创建新节点
   
    
    while(cur)
    {
        struct Node* ret=(struct Node*)malloc(sizeof(struct Node));
        ret->val=cur->val;
        ret->next=cur->next;
        cur->next=ret;
        cur=ret->next;
    }
    cur=head; 
    //改变random指向
    while(cur)
    {
        struct Node* obj=cur->next;
        if(cur->random==NULL)
        {
            obj->random=NULL;
        }
        else
        {
            obj->random=cur->random->next;
        }
        
        cur=obj->next;
        
    }
    cur=head->next;
    //将新节点剥离
    while(cur->next)
    {
        cur->next=cur->next->next;
        cur=cur->next;
    }
    return head->next;
}


(3)

(4)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/7960.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

一般人我劝你还是要不自学软件测试.....

软件测试基础真的很简单&#xff0c;是个人稍微认真点都能懂&#xff0c;这就是好多人说软件测试简单、易懂、好学&#xff0c;然后就是一顿浮夸的言论&#xff0c;误导那些小白&#xff0c;这里我就给那些轻浮的人泼一桶冷水&#xff0c;懂和学会是一码事吗&#xff1f; 这里…

如何高效填写软件测试缺陷报告?

软件缺陷的描述是软件缺陷报告的基础部分&#xff0c;需要使用简单、准确、专业的术语来描述缺陷。否则&#xff0c;它就会含糊不清&#xff0c;可能会误导开发人员&#xff0c;影响开发人员的效率&#xff0c;也会影响测试人员自身的声誉&#xff0c;准确报告缺陷是非常重要的…

[附源码]java毕业设计基于SSM的酒店管理系统

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

高新技术企业的认定条件(三)

十、企业创新能力评价 企业创新能力主要从知识产权、科技成果转化能力、研究开发组织管理水平、企业成长性等四项指标进行评价。各级指标均按整数打分&#xff0c;满分为100分&#xff0c;综合得分达到70分以上&#xff08;不含70分&#xff09;为符合认定要求。具体分值如下&…

opencv4.5.5安装的坑

opencv4.5.5安装的坑 Package opencv was not found in the pkg-config search path. Perhaps you should add the directory containing opencv.pc’ to the PKG_CONFIG_PATH environment variable No package ‘opencv’ found 我把对应目录的opencv4.pc修改成了opencv.pc,运…

记录<一个多SDK中引入ffmpeg出现的top1的crash问题>解决方案

文章目录一. 背景二. 分析、定位crash源代码三. 分析汇编四. 分析源码五. 思考一. 背景 App从某个版本开始突然收到一例高频crash&#xff0c;&#xff0c;crash信息如下 crash栈如下&#xff1a; crash栈信息很少&#xff0c;只能看出是线程刚启动就crash了&#xff0c;内存…

Java【类和对象】,你还没对象?我教你new一个!

文章目录前言一、初识面向对象1.什么是面向对象2.面向对象过程二、类的定义与使用1.什么是类2.如何定义类3.示例三、类的实例化1.什么是类的实例化2.类和对象的说明四、this引用1.为什么要有this引用第一种情况第二种情况2.什么是this引用3.this引用的特征五、对象的构造及初始…

ubuntu上运行make menuconfig两种报错

1&#xff09; 如果如上报错&#xff0c;没有target&#xff0c;这是因为运行这个make的时候没在对应的目录下运行&#xff0c;通常是有make的目录&#xff0c;这种系统性的编译的&#xff0c;则是应该在代码的根目录。在根目录下用根目录下的make。 2&#xff09;然后我来到根…

Restful 接口设计-前言(手把手教你入门到精通)

文章目录前言一&#xff1a;什么是API分类一分类二前言二&#xff1a;Web的发展前言三&#xff1a;传统开发模式VS前后端分离传统的开发模式前后端分离了解API和Web的发展有利于帮助你掌握Restful接口设计 前言一&#xff1a;什么是API API&#xff08;Application Programmi…

第三章 多维随机变量及其分布

思维导图 基础知识 二维随机变量 我们研究一个多维的东西&#xff0c;往往先从较低的维度比如说二维作为主要的研究对象&#xff0c;一个是因为维度低会比较简单&#xff0c;易于理解&#xff1b;另一个则是考试中低维的问题往往更加常见 定义与分布函数 定义上其实很简单&am…

双亲委派机制

Java 虚拟机对 class 文件采用的是按需加载的方式&#xff0c;也就是说当需要使用该类时才会将它的 class 文件加载到内存生成 class 对象。而且加载某个类的 class 文件时&#xff0c;Java 虚拟机采用的是双亲委派模式&#xff0c;即把请求交由父类处理&#xff0c;它是一种任…

【Pytorch with fastai】第 6 章 :其他计算机视觉问题

&#x1f50e;大家好&#xff0c;我是Sonhhxg_柒&#xff0c;希望你看完之后&#xff0c;能对你有所帮助&#xff0c;不足请指正&#xff01;共同学习交流&#x1f50e; &#x1f4dd;个人主页&#xff0d;Sonhhxg_柒的博客_CSDN博客 &#x1f4c3; &#x1f381;欢迎各位→点赞…

【MySQL】数据库中表的增删查改操作详解

文章目录前言SQL的通用语法一、表的创建与表的新增语法数据类型的介绍演示二、表的删除语法删整张表的语法删记录的语法演示三、表的查询查询整张表&#xff08;一&#xff09;全列查询&#xff08;二&#xff09;指定列查询&#xff08;三&#xff09;带表达式的查询&#xff…

深入ftrace function graph功能

学习完了ftrace的function的基本功能&#xff0c;其作用主要是用来跟踪特定内核函数调用的频次&#xff0c;对于内核&#xff0c;特别是初学者&#xff0c;对于函数的调用关系不清晰&#xff0c;并且内核中有很多函数指针&#xff0c;会把我们弄的摸不着头脑&#xff0c;那么我…

C语言百日刷题第九天

前言 今天是刷题第9天&#xff0c;放弃不难&#xff0c;但坚持一定很酷~ 快来跟我一起刷题吧。 加油&#xff0c;一起努力进步 C语言百日刷题第九天前言76.计算偶数的所有质因子77. 提取不重复的整数78.二进制中1的个数79.猴子分桃80.百钱买百鸡76.计算偶数的所有质因子 输入…

可防离职员工冒用身份,合合信息名片全能王与钉钉用数字名片打造安全“围栏”

名片全能王与钉钉发布数字名片&#xff1a;可防离职员工冒用身份&#xff0c;追踪营销线索 名片是人际交往中的一条纽带。秦汉有竹制的“谒”&#xff0c;唐代纸质的“名刺”也被沿用许久。如今&#xff0c;无实体的数字名片在商务人士中已十分普及&#xff0c;科技加持下&…

共享存储知识

文章目录一、架构图二、RAID&#xff08;廉价冗余磁盘整列&#xff09;三、存储操作四、FusionCompute对接存储五、添加数据存储时的选项六、磁盘配置模式七、磁盘模式八、快照九、FC-SAN一、架构图 SAN&#xff08;存储区域网络&#xff09; IP-SAN&#xff08;SCSI协议封装在…

Linux多线程C++版 线程基础 进程和线程关系 线程分类 Linux线程实现 线程表示

目录1.线程的基础2.进程和线程的关系3.线程分类 了解4.Linux线程实现5.线程标识1.线程的基础 进程是资源管理的最小单位&#xff0c;线程是程序执行最小单位。每个进程有自己的数据段&#xff0c;代码段和堆栈段。线程通常叫做轻型的进程&#xff0c;它包含独立的栈和CPU寄存器…

[附源码]java毕业设计基于的楼盘销售管理系统论文2022

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

SpringCloud Gateway 自定义Filter用代码怎么写?别再说我只会配置基本Filter了,来学学如何定制Filter过滤器

文章目录学习SpringCloud Gateway自定义全局Filter&#xff08;GlobalFilter&#xff09;ServerWebExchange自定义非全局Filter&#xff08;GatewayFilter&#xff09;学习SpringCloud Gateway SpringCloud Gateway使用及原理分析大全——断言及过滤器&#xff08;上篇&#x…