详解单链表经典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

目录
相关文章
|
存储 C语言 C++
|
自然语言处理 程序员 数据库
心得经验总结:浅谈文字编码和unicode(下)
心得经验总结:浅谈文字编码和unicode(下)
131 0
|
C++
【PAT甲级 - C++题解】1027 Colors in Mars
【PAT甲级 - C++题解】1027 Colors in Mars
155 0
|
存储 安全 专有云
全网首发 | 阿里云存储产品手册
阿里云存储产品手册正式发布会
451 0
全网首发 | 阿里云存储产品手册
Java状态模式(State)
现实生活中我们经常会碰到状态改变的场景,面对不同的场景我们会做出不同的处理
Java状态模式(State)
|
数据库 Python
统计各个分类和标签下的文章数
在我们的博客侧边栏有分类列表和标签列表,显示博客已有的全部文章分类。现在想在分类名和标签名后显示该分类或者标签下有多少篇文章,该怎么做呢?最优雅的方式就是使用 django 的 annotate 方法。 Model 回顾 回顾一下我们的 model 代码,django 博客有一个 Post 和 Category 模型,分别表示文章和分类: blog/models.py class Post(models.Model): title = models.CharField('标题', max_length=70) body = models.TextField('正文')
195 0
《社交网站界面设计(原书第2版)》——2.5 严格 VS. 灵活的分类法
本节书摘来自华章计算机《社交网站界面设计(原书第2版)》一书中的第2章,第2.5节,作者:(美)克里斯蒂安·克鲁姆里什(Christian Crumlish),艾琳·马洛恩(Erin Malone)著, 更多章节内容可以访问云栖社区“华章计算机”公众号查看。
1054 0
|
Web App开发 前端开发 开发工具
微软良心之作——Visual Studio Code 开源免费跨平台代码编辑器
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chinahuyong/article/details/46480995 微软良...
1643 0

热门文章

最新文章