news 2026/4/28 18:44:26

【简单】在双链表中删除倒数第K个节点-Java

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【简单】在双链表中删除倒数第K个节点-Java

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击人工智能教程大家好!欢迎来到我的网站! 人工智能被认为是一种拯救世界、终结世界的技术。毋庸置疑,人工智能时代就要来临了,科… 继续阅读 前言https://www.captainai.net/troubleshooter

package live.every.day.ProgrammingDesign.CodingInterviewGuide.List; /** * 在双链表中删除倒数第K个节点 * * 【题目】 * 实现一个函数,删除双链表中倒数第K个节点。 * * 【要求】 * 如果链表长度为N,时间复杂度达到O(N),额外空间复杂度达到O(1)。 * * 【难度】 * 简单 * * 【解答】 * 对于双链表的调整,几乎与单链表的处理方式一样,注意last指针的重连即可。 * 具体过程请参看如下代码中的removeLastKthNode方法。 * * @author Created by LiveEveryDay */ public class RemoveLastKthNodeInDoubleList { public static class DoubleNode { public int data; public DoubleNode prev; public DoubleNode next; public DoubleNode(int data) { this.data = data; } } public static DoubleNode removeLastKthNode(DoubleNode head, int k) { if (head == null || k < 1) { return head; } DoubleNode cur = head; while (cur != null) { k--; cur = cur.next; } if (k > 0) { } else if (k == 0) { head = head.next; head.prev = null; } else { cur = head; while (++k != 0) { cur = cur.next; } DoubleNode node = cur.next.next; cur.next = node; if (node != null) { node.prev = cur; } } return head; } public static void main(String[] args) { DoubleNode node1 = new DoubleNode(1); DoubleNode node2 = new DoubleNode(2); DoubleNode node3 = new DoubleNode(3); DoubleNode node4 = new DoubleNode(4); DoubleNode node5 = new DoubleNode(5); node1.next = node2; node2.next = node3; node3.next = node4; node4.next = node5; removeLastKthNode(node1, 4); DoubleNode n = node1; while (n != null) { System.out.printf("%d ", n.data); n = n.next; } } } // ------ Output ------ /* 1 3 4 5 */
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/28 18:43:02

云音乐歌词提取终极指南:3步获取LRC歌词的完整解决方案

云音乐歌词提取终极指南&#xff1a;3步获取LRC歌词的完整解决方案 【免费下载链接】163MusicLyrics 云音乐歌词获取处理工具【网易云、QQ音乐】 项目地址: https://gitcode.com/GitHub_Trending/16/163MusicLyrics 还在为找不到心爱歌曲的LRC歌词而烦恼吗&#xff1f;1…

作者头像 李华
网站建设 2026/4/28 18:43:01

如何让普通声卡也能拥有专业级ASIO低延迟音频体验?

如何让普通声卡也能拥有专业级ASIO低延迟音频体验&#xff1f; 【免费下载链接】FlexASIO A flexible universal ASIO driver that uses the PortAudio sound I/O library. Supports WASAPI (shared and exclusive), KS, DirectSound and MME. 项目地址: https://gitcode.com…

作者头像 李华
网站建设 2026/4/28 18:35:20

3分钟极速配置黑苹果:OpCore Simplify图形化工具完全指南

3分钟极速配置黑苹果&#xff1a;OpCore Simplify图形化工具完全指南 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 还在为复杂的OpenCore配置而烦恼…

作者头像 李华
网站建设 2026/4/28 18:32:53

OpCore Simplify实战指南:高效自动化OpenCore EFI配置的最佳实践

OpCore Simplify实战指南&#xff1a;高效自动化OpenCore EFI配置的最佳实践 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify OpCore Simplify是一款专…

作者头像 李华
网站建设 2026/4/28 18:32:52

rope集成VSCode与PyCharm:在IDE中实现智能重构

rope集成VSCode与PyCharm&#xff1a;在IDE中实现智能重构 【免费下载链接】rope a python refactoring library 项目地址: https://gitcode.com/gh_mirrors/rop/rope Rope是一款强大的Python重构库&#xff0c;能够帮助开发者在VSCode与PyCharm等主流IDE中实现智能重构…

作者头像 李华