• 单链表经典例题


    移除链表元素

    题目描述:
    在这里插入图片描述
    ➡️挑战链接⬅️、
    分析:
    该题是要求我们删除指定元素,那么我们就定义一个cur指针去遍历整个链表就好了,每当我们遇到cur->val==val;等于特定值的时候我们就释放掉该节点就可以了;
    如果我们遇到的不是特定值,则直接将该节点摘下来,即可;
    当然,既然摘下来了,就不能让这些节点,孤单着,我门内就得找一个“线”捡起链接起来;
    最后返回新头就行了;
    这里我们采取不带哨兵位的方法讲解一下:
    在这里插入图片描述
    代码实现:

    struct ListNode* removeElements(struct ListNode* head, int val){
             struct ListNode*Newhead=NULL;
             struct ListNode*tail=NULL;
             struct ListNode*cur=head;
             struct ListNode*next=NULL;
             while(cur)
             {
                 if(cur->val==val)
                 {
                     next=cur->next;
                     free(cur);
                     cur=next;
                 }
                 else 
                 {
                     if(tail==NULL)
                     {
                         next=cur->next;
                         Newhead=tail=cur;
                         tail->next=NULL;
                         cur=next;
                     }
                     else
                     {
                         next=cur->next;
                         tail->next=cur;
                         tail=cur;
                         tail->next=NULL;
                         cur=next;
                     }
                 }
             }
             return Newhead;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    在这里插入图片描述
    时间复杂度:O(N)
    空间复杂度:O(1)

    如果采用带哨兵位的头节点的话,我们就不用考虑特殊情况,直接就能在tail尾部插入,代码自然也会简短一点;
    代码实现:

    struct ListNode* removeElements(struct ListNode* head, int val){
               struct ListNode*dummyhead=(struct ListNode*)malloc(sizeof(struct ListNode));
               struct ListNode*prev=dummyhead;
               dummyhead->next=head;
               struct ListNode*cur=head;
               while(cur)
               {
                   if(cur->val!=val)
                   {
                       prev=cur;
                       cur=cur->next;
                   }
                   else 
                   {
                       struct ListNode*next=cur->next;
                       prev->next=next;
                       cur=next;
                   }
               }
               struct ListNode*newHead=dummyhead->next;
               free(dummyhead);
               return newHead;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    反转链表

    题目描述:
    在这里插入图片描述
    ➡️挑战链接⬅️
    分析:
    反转链表,我们可以遍历一遍单链表,然后将每个单链表利用尾插的方式实现,就能达到链表反转的结果了:
    在这里插入图片描述
    代码实现:

    struct ListNode* reverseList(struct ListNode* head){
             struct ListNode*retHead=NULL;
             struct ListNode*cur=head;
             struct ListNode*next=NULL;
             while(cur)
             {
                 next=cur->next;
                 cur->next=retHead;
                 retHead=cur;
                 cur=next;
             }
             return retHead;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    时间复杂度:O(N)
    空间复杂度:O(1)

    在这里插入图片描述

    合并两个有序链表

    题目描述:
    在这里插入图片描述
    ➡️挑战链接⬅️
    分析:
    题目给了一个很好的条件,链表有序,那么我们就可以根据归并排序的思想,对于链表进行合并;
    我们同时对于连个链表进行遍历,没遇到val值,比较小的节点,我们就把它采摘下来;同时完成对于采摘下来的节点进行重新链接;
    我们采取不带哨兵位实现,带哨兵位读者可以自行研究一下😊😊😊)
    在这里插入图片描述
    代码实现:

    struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){    
                struct ListNode*Newhead=NULL;
                struct ListNode*tail=NULL;
                struct ListNode*l1=list1;
                struct ListNode*l2=list2;
                struct ListNode*next=NULL;
                while(l1&&l2)
                {
                   if(l1->val>l2->val)
                   {
                     next=l2->next;
                       if(tail==NULL)
                       {
                       Newhead=tail=l2;
                       }
                       else 
                       {
                           tail->next=l2;
                           tail=l2;
                       }
                       tail->next=NULL;
                           l2=next;
                   }
                   else
                   {
                     next=l1->next;
                       if(tail==NULL)
                       {
                       Newhead=tail=l1;
                       }
                       else 
                       {
                           tail->next=l1;
                           tail=l1;
                       }
                       tail->next=NULL;
                           l1=next;
                   }
                }
                if(l1)
                  {
                      if(tail==NULL)
                      Newhead=l1;
                      else
                      tail->next=l1;
                  }
                if(l2)
                {
                      if(tail==NULL)
                      Newhead=l2;
                      else
                      tail->next=l2;
                  }
                return Newhead;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    在这里插入图片描述
    其实一道题需不需要带哨兵位来解决,主要还是看头节点有没有可能会被改变,如果有可能,我们就需要添加哨兵位的头节点来帮忙,这样我们就可以避免讨论很多的特殊情况;反之,我们就没比要构造哨兵位的头节点;

  • 相关阅读:
    6-21漏洞利用-mysql弱口令破解
    kubernetes(K8S)学习笔记P1:基本概念和架构
    高压放大器在软体机器人领域的应用
    Socks5代理IP在网络安全、跨境电商和游戏中的应用
    CopyOnWriteArrayList 是如何保证线程安全的?
    《省级国土空间规划编制技术规程》国家标准(GB/T 43214-2023)原文下载
    信息化发展64
    kubeadm系列-02-kubelet的配置和启动
    MySQL子查询
    canvas画布中beginPath和closePath的作用要点
  • 原文地址:https://blog.csdn.net/qq_62106937/article/details/127698217