详解单链表经典OJ题-1

简介: 详解单链表经典OJ题

前言

本篇文章主要就是讲述数据结构中链表有关的一些经典的OJ题,文末也给大家提供了OJ题的具体网址供大家练习,通过这些OJ题希望对你理解链表有一个更深层次的理解,最后创作不易,希望大家能够给予鼓励,点点赞,评论交流学习!

一 删除链表中等于给定值“val”的所有节点

例如:

输入:head = [1,2,6,3,6], val = 6

输出:[1,2,3]

图解过程:

删除之后

思路有了之后,代码如下:

    public nodelist deletion(int val){
        if (head==null)return null;
        nodelist cur=head;
        while (cur!=null){
            while(cur.next!=null&&cur.next.val==val){
                cur.next=cur.next.next;
            }
            cur=cur.next;
        }
        if(head.val==val){
            head=head.next;
        }
        return head;
    }

二 反转一个单链表

例如:

输入:head = [1,2,3,4,5]

输出:[5,4,3,2,1]

代码具体实现

    public nodelist fanzhuang(){
        nodelist prve=null;
        nodelist cur=head.next;
        while (cur!=null){
            head.next=prve;
            prve=head;
            head=cur;
            cur=cur.next;
        }
        head.next=prve;
        return head;
    }

三 求中间节点

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

例如:

head=[1,2,3,4,5] 返回3

head=[1,2,3,4] 返回3

图解分析

代码实现:

    //中间节点问题
    //解决方法快慢指针
    public nodelist midnode(){
        nodelist fast=head;
        nodelist slow=head;
        while (fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        return slow;
    }

对于两个节点的分析方法可以自己画图体会,就能更好的理解这个题目。在这里我们提出一个变式,如果有两个节点,我们要求要返回第一个节点该怎么做?

具体的代码实现如下:

    public nodelist midnode(){
        nodelist fast=head;
        nodelist slow=head;
        while (fast!=null&&fast.next!=null){
            fast=fast.next.next;
            if (fast==null){
                return slow;
            }
            slow=slow.next;
           
        }
        return slow;
    }

四 链表倒数第K个节点

具体代码实现:

//求倒数第k个节点
    public nodelist node(int k){
        if (k<0||head==null)return null;
        nodelist slow=head;
        nodelist fast=head;
        while (k-1!=0){
            fast=fast.next;
            if (fast==null){//如果超出了范围,那么fast应该是一个null
                return null;
            }
            k--;
        }
        while (fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }

五 合并有序链表

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

对于这种题目我们可以采用,创建一个新的节点,然后把他们都给串起来具体实现代码如下:

    //合并有序列表
    public static nodelist mergeTwoLists(nodelist head, nodelist head1) {
        nodelist newhead=new nodelist(-1);//创建一个新的节点
        nodelist cur=newhead;
        while(head!=null&&head1!=null){
            if(head.val<head1.val){
                cur.next=head;
                cur=head;
                head=head.next;
            }else{
                cur.next=head1;
                cur=head1;
                head1=head1.next;
            }
        }
        if(head==null){
            cur.next=head1;
        }else{
            cur.next=head;
        }
        return newhead.next;
    }


详解单链表经典OJ题-2

https://developer.aliyun.com/article/1504001

目录
相关文章
|
8月前
【LeetCode】【数据结构】单链表OJ常见题型(一)
【LeetCode】【数据结构】单链表OJ常见题型(一)
53 0
|
8月前
【LeetCode】【数据结构】单链表OJ常见题型(二)
【LeetCode】【数据结构】单链表OJ常见题型(二)
38 0
【LeetCode】——链式二叉树经典OJ题详解
在之前的文章讲解了二叉树的链式结构的实现以及前、中、后序的遍历。不知道大家看完后有没有理解和有所收获,今天基于那篇文章给大家分享讲解几道经典的题目,更便于大家的理解和深入。
|
5天前
|
机器学习/深度学习 算法 C语言
【C/数据结构与算法】:10道链表经典OJ
【C/数据结构与算法】:10道链表经典OJ
9 1
|
4天前
|
算法 C语言
【数据结构与算法 经典例题】链表的回文结构(图文详解)
【数据结构与算法 经典例题】链表的回文结构(图文详解)
|
1月前
|
算法
详解单链表经典OJ题-2
详解单链表经典OJ题
17 2
|
1月前
|
存储
栈与队列相关OJ题
栈与队列相关OJ题
12 0
|
1月前
|
索引
单链表经典OJ题(四)
单链表经典OJ题(四)
|
1月前
|
索引
单链表经典OJ题(三)
单链表经典OJ题(三)
|
1月前
单链表经典OJ题(二)
单链表经典OJ题(二)