7月8日 四道经典单链表oj题

news/2024/7/24 2:40:10 标签: java, java-ee, leetcode

大家好呀,本博客目的在于记录暑假学习打卡,后续会整理成一个专栏,主要打算在暑假学习完数据结构,因此会发一些相关的数据结构实现的博客和一些刷的题,个人学习使用,也希望大家多多支持,有不足之处也请指出,谢谢大家。

一,203.移除链表元素

. - 力扣(LeetCode)

方法一:定义新链表

java">/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null)
            return head;
        ListNode newtail = head;
        ListNode newhead = new ListNode();
        ListNode cur = newhead;
        while (newtail != null) {
            if (newtail.val != val) {
                cur.next = newtail;
                cur = newtail;
                newtail = newtail.next;
            } else {
                if (newtail.next == null)
                    cur.next = null;
                newtail = newtail.next;
            }
        }
        return newhead.next;
    }
}

方法二:在原链表基础上删除

java">/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null)
            return head;
        ListNode prev = head;
        ListNode pcur = head.next;
        while (pcur != null) {
            if (pcur.val == val) {
                prev.next = pcur.next;
                pcur = pcur.next;
            } else {
                prev = pcur;
                pcur = pcur.next;

            }

        }
        if(head.val==val)
        head=head.next;
        return head;
    }
}

二,206.反转链表

. - 力扣(LeetCode)

采用了把后面的节点头插到头节点前面的方式

java">/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null)
            return head;
        ListNode cur = head.next;
        head.next = null;
        ListNode curN;
        while (cur != null) {
            curN = cur.next;

            cur.next = head;
            head = cur;
            cur = curN;
        }
        return head;

    }
}

三,876链表的中间节点

. - 力扣(LeetCode)

运用了快慢指针

java">/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        if(head==null)
        return head;
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null)
        {
            fast=fast.next.next;
            slow=slow.next;
        }
        return slow;

    }
}

四,面试题02.02 返回倒数第k个节点

. - 力扣(LeetCode)

采用快慢指针

java">/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int kthToLast(ListNode head, int k) {
        if(head==null)
        return -1;
        ListNode fast=head;
        ListNode slow=head;
        int count=0;
        while(count!=k-1){
            fast=fast.next;
            count++;
        }
        while(fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow.val;
    }
}

本期博客就到这里,感谢阅读


http://www.niftyadmin.cn/n/5546941.html

相关文章

算法学习记录3

L1-077 大笨钟的心情 mood_levels list(map(int, input().split()))# 存储询问时间点 lines []# 获取询问时间点,直到遇到非法输入 while True:ask_time input()if ask_time "":breaklines.append(int(ask_time))# 遍历询问时间点并判断对应心情指数 …

【力扣: 15题: 三数之和】

15题: 三数之和 给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k ,同时还满足 nums[i] nums[j] nums[k] 0 。请 你返回所有和为 0 且不重复的三元组。 注意: 答案中不可以包含重复的三元组。 …

初步理解三__《面向互联网大数据的威胁情报 并行挖掘技术研究》

初步理解三 5类战术标签 gtp 收集开源的网络安全报告并将其转化为统一的文本格式,并且标注了5类战术标签是一个涉及到数据处理和分类的复杂任务。以下是一种可能的处理方法: 数据收集和整合: 使用网络爬虫或API访问工具收集开源的网络安全…

JAVA的String的不可变特性

在学习JAVA的时候,看到了JAVA的String具有不可变的特性,他是说,JAVA的String在创建好后,JVM将这个String变量指向内存中的一个地址,当下次改变这个String变量的时候,改变的不是这个变量的值,而是…

jcmd命令笔记

文章目录 GC.class_statsjcmd 25274 Thread.printjcmd 25274 GC.run 其他文档(命令行) jcmd是一款命令行工具,可以监控jvm虚拟机性能和诊断问题。 GC.class_stats 如果报错: GC.class_stats command requires -XX:UnlockDiagnosticVMOptions 在启动脚本…

鸿蒙开发:Universal Keystore Kit(密钥管理服务)【密钥协商(ArkTS)】

密钥协商(ArkTS) 以协商密钥类型为X25519 256,并密钥仅在HUKS内使用为例,完成密钥协商。 开发步骤 生成密钥 设备A、设备B各自生成一个非对称密钥,具体请参考[密钥生成]或[密钥导入]。 密钥生成时,可指定参数HUKS_TAG_DERIVE…

mysql实战入门-基础篇

目录 1、MySQL概述 1.1、数据库相关概念 1.2、MySQL数据库 1.2.1、版本 1.2.2、下载 1.2.3、安装 输入MySQL中root用户的密码,一定记得记住该密码 1.2.4、启动停止 1.2.5、客户端连接 1.2.6、数据模型 2、SQL 2.1、SQL通用语法 2.2、SQL分类 2.3、DDL 2.3.1、数据…

谷粒商城学习笔记-踩坑合集

1,Idea新增Module报错:sdk ‘1.8‘ type ‘JavaSDK‘ is not registered in ProjectJdkTable 2,IDEA创建Spring项目无法使用Java8的解决方案 3,谷粒商城-记录创建工程和模块时遇到的两个问题 4,谷粒商城学习笔记-使用r…