在单链表和双链表中删除倒数第k个节点

2024-06-21 22:38

本文主要是介绍在单链表和双链表中删除倒数第k个节点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实现的完整代码如下:

//在单链表和双链表中删除倒数第k个节点public class DeleteList{//单链表节点的定义public static class Node{int value;Node next;public Node(int data){this.value=data;}}//删除单链表中倒数第k个节点public static int DeletList_K(Node head,int k){   Node t=head; //用t指向链表的头结点if(head==null||k<1){return -10000;  //表示没有删除的数据}int leng=0; //计算链表的长度while(t!=null){t=t.next;++leng;}//System.out.println(leng); 打印出链表的长度为3if(k>leng){return -10000;  //表示没有删除的数据}for(int i=0;i<leng-k-1;i++){head=head.next;  //找到倒数第k-1个节点}Node p=head.next;  //找到倒数第k个节点  head.next=head.next.next; //删除倒数第k个节点return p.value;}//双链表节点的定义public static class Node2{int value;Node2 next,pre;public Node2(int data){this.value=data;}}//删除双链表中倒数第k个节点public static int DelteTwoList_K(Node2 head2,int k){Node2 t=head2; //用t指向链表的头结点if(head2==null||k<1){return -10000;}int leng=0; //计算双链表的长度while(t!=null){t=t.next;++leng;}//System.out.println(leng); //打印出链表的长度为3if(k>leng){return -10000;  //表示没有删除的数据}for(int i=0;i<leng-k;i++){head2=head2.next;}Node2 p=head2; //倒数第k个节点p.pre.next=p.next;  //删除倒数第k个节点return p.value;}public static void main(String []args){//删除单链表倒数第k个节点测试  链表为 1->2->3Node node=new Node(1);node.next=new Node(2);node.next.next=new Node(3);//node.next.next.next=new Node(4);System.out.println(DeletList_K(node,2));//删除双链表倒数第k个节点测试  链表为 1->2->3Node2 node2=new Node2(1);node2.next=new Node2(2);node2.next.next=new Node2(3);node2.next.pre=node2;node2.next.next.pre=node2.next;//node.next.next.next=new Node(4);System.out.println(DelteTwoList_K(node2,2));}
}

左神的代码:

public class Problem_02_RemoveLastKthNode {public static class Node {public int value;public Node next;public Node(int data) {this.value = data;}}public static Node removeLastKthNode(Node head, int lastKth) {if (head == null || lastKth < 1) {return head;}Node cur = head;while (cur != null) {lastKth--;cur = cur.next;}if (lastKth == 0) {head = head.next;}if (lastKth < 0) {cur = head;while (++lastKth != 0) {cur = cur.next;}cur.next = cur.next.next;}return head;}public static class DoubleNode {public int value;public DoubleNode last;public DoubleNode next;public DoubleNode(int data) {this.value = data;}}public static DoubleNode removeLastKthNode(DoubleNode head, int lastKth) {if (head == null || lastKth < 1) {return head;}DoubleNode cur = head;while (cur != null) {lastKth--;cur = cur.next;}if (lastKth == 0) {head = head.next;head.last = null;}if (lastKth < 0) {cur = head;while (++lastKth != 0) {cur = cur.next;}DoubleNode newNext = cur.next.next;cur.next = newNext;if (newNext != null) {newNext.last = cur;}}return head;}public static void printLinkedList(Node head) {System.out.print("Linked List: ");while (head != null) {System.out.print(head.value + " ");head = head.next;}System.out.println();}public static void printDoubleLinkedList(DoubleNode head) {System.out.print("Double Linked List: ");DoubleNode end = null;while (head != null) {System.out.print(head.value + " ");end = head;head = head.next;}System.out.print("| ");while (end != null) {System.out.print(end.value + " ");end = end.last;}System.out.println();}public static void main(String[] args) {Node head1 = new Node(1);head1.next = new Node(2);head1.next.next = new Node(3);head1.next.next.next = new Node(4);head1.next.next.next.next = new Node(5);head1.next.next.next.next.next = new Node(6);printLinkedList(head1);head1 = removeLastKthNode(head1, 3);// head1 = removeLastKthNode(head1, 6);// head1 = removeLastKthNode(head1, 7);printLinkedList(head1);DoubleNode head2 = new DoubleNode(1);head2.next = new DoubleNode(2);head2.next.last = head2;head2.next.next = new DoubleNode(3);head2.next.next.last = head2.next;head2.next.next.next = new DoubleNode(4);head2.next.next.next.last = head2.next.next;head2.next.next.next.next = new DoubleNode(5);head2.next.next.next.next.last = head2.next.next.next;head2.next.next.next.next.next = new DoubleNode(6);head2.next.next.next.next.next.last = head2.next.next.next.next;printDoubleLinkedList(head2);head2 = removeLastKthNode(head2, 3);// head2 = removeLastKthNode(head2, 6);// head2 = removeLastKthNode(head2, 7);printDoubleLinkedList(head2);}}



这篇关于在单链表和双链表中删除倒数第k个节点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1082541

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

如何在Mac上彻底删除Edge账户? 手动卸载Edge浏览器并清理残留文件技巧

《如何在Mac上彻底删除Edge账户?手动卸载Edge浏览器并清理残留文件技巧》Mac上的Edge账户里存了不少网站密码和个人信息,结果同事一不小心打开了,简直尴尬到爆炸,想要卸载edge浏览器并清... 如果你遇到 Microsoft Edge 浏览器运行迟缓、频繁崩溃或网页加载异常等问题,可以尝试多种方

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja