链表OJ题

今天讲一些关于链表的Oj题,相信你看完对链表又提升一个档次。

题目一

思路一

遍历一遍链表是Val值得时候free这个,然后我们往后走,一直走到末尾空指针得时候,新链表就是我们得答案,那我们用代码来表示一下吧。

c 复制代码
struct ListNode* removeElements(struct ListNode* head, int val){
    struct ListNode*cur=head;
    struct ListNode*pre=NULL;
    
   while(cur)
   {
       if(cur->val==val)
       {
           if(pre==NULL)
           {
               head=cur->next;
               free(cur);
               cur=head;
           }
          else
          {
             pre->next=cur->next;
           free(cur);
           cur=pre->next;
          }
       }
       else
       {
           pre=cur;
           cur=cur->next;
       }
   }
   return head;
}

思路二

不是val我们就拿下来,是val就跳过,放到新链表中。

c 复制代码
struct ListNode* removeElements(struct ListNode* head, int val){
        struct ListNode*tail=NULL;
        struct ListNode*cur=head;
       
        head=NULL;
        while(cur)
        {
            if(cur->val==val)
            {
                cur=cur->next;
            }
            else
            {
                if(tail==NULL)
                {
                    head=tail=cur;
                }
                else
                {
                    tail->next=cur;
                    tail=tail->next;
                }
                 cur=cur->next;
            }
        }
        if(tail)
          tail->next=NULL;   

    return head;
}

思路三

带哨兵位得头节点得方法,是val拿下来,不是跳过。

c 复制代码
struct ListNode* removeElements(struct ListNode* head, int val){
       struct ListNode*cur=head;
       head=(struct ListNode*)malloc(sizeof(struct ListNode));
       struct ListNode*tail=head;
     tail->next=NULL;
     while(cur)
     {
         if(cur->val==val)
         {
             struct ListNode*del=cur;
            cur=cur->next;
            free(del);
         }
         else
         {
             tail->next=cur;
             cur=cur->next;
             tail=tail->next;
         }
     }
     tail->next=NULL;
        struct ListNode*del=head->next;
        free(head);
        return del;
    
}

题目二

这题我们可以改变指向,给三个指针变量,变方向就可以解决。

c 复制代码
struct ListNode* reverseList(struct ListNode* head){
       if(head==NULL)
        return NULL;
        struct ListNode*cur=head;
        struct ListNode*pre=NULL;
        struct ListNode*next=cur->next;
        while(cur)
        {
            cur->next=pre;
            pre=cur;
            cur=next;
            if(next)
                next=next->next;
        }
        return pre;
}

思路二

头插到新链表就可以了。

c 复制代码
struct ListNode* reverseList(struct ListNode* head){
        struct ListNode*cur=head;
        struct ListNode*phead=NULL;
        while(cur)
        {
            struct ListNode*next=cur->next;
            cur->next=phead;
            phead=cur;
            cur=next;
        }
        return phead;
}

题目三

用快慢指针,快走两步,慢走一步,就可以解决。

c 复制代码
struct ListNode* middleNode(struct ListNode* head){
        struct ListNode*slow=head;
        struct ListNode*fast=head;
        while(fast && fast->next)
        {
            slow=slow->next;
            fast=fast->next;
            if(fast)
            fast=fast->next;
        }
        return slow;
}

题目四

c 复制代码
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
    struct ListNode*slow=pListHead;
     struct ListNode*fast=pListHead;
     
     while((k--))
     {
        if(fast==NULL)
            return NULL;
        fast=fast->next;
     }
     while(fast)
     {
        fast=fast->next;
        slow=slow->next;
     }
     return slow;
}

这题和快慢指针差不多,先让快指针走k步,然后同时走,结束条件就是快指为空的时候。

今天就先分享四道题目,后面再继续分享几道!!!谢谢大家观看

相关推荐
liu****12 小时前
10.queue的模拟实现
开发语言·数据结构·c++·算法
shinelord明13 小时前
【大数据技术实战】Kafka 认证机制全解析
大数据·数据结构·分布式·架构·kafka
让我们一起加油好吗13 小时前
【基础算法】01BFS
数据结构·c++·算法·bfs·01bfs
草莓工作室13 小时前
数据结构3:线性表2-顺序存储的线性表
数据结构·windows
1白天的黑夜114 小时前
递归-24.两两交换链表中的节点-力扣(LeetCode)
数据结构·c++·leetcode·链表·递归
1白天的黑夜114 小时前
递归-206.反转链表-力扣(LeetCode)
数据结构·c++·leetcode·链表·递归
靠近彗星14 小时前
3.1 栈
数据结构·算法
sulikey14 小时前
一文彻底理解:如何判断单链表是否成环(含原理推导与环入口推算)
c++·算法·leetcode·链表·floyd·快慢指针·floyd判圈算法
孤廖15 小时前
吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
java·开发语言·数据结构·c++·人工智能·深度学习·算法
豆沙沙包?16 小时前
2025年--Lc197-077. 排序链表(链表,尾插法)--Java版
java·数据结构·链表