博客
关于我
查找单链表中倒数第k个节点
阅读量:623 次
发布时间:2019-03-14

本文共 3561 字,大约阅读时间需要 11 分钟。

在这里插入图片描述

问题:

查找单链表倒数第k个节点

解决方案:

思路

先获取链表的长度length,判断k的合法性,以及k的位置

要找的节点就在length-k处(视情况)

查找倒数第k个节点

/**     * 查找倒数第k个节点     * @param head 链表的头节点     * @param index  表示要查找结点的逆序下标     * @return 返回倒数第index个个点     */    public Node getK(Node head,int index){           if (head.next == null){   //链表为空            return null;        }        int length = getLength(head);//获取链表的长度        if (index <=0 || index > length){   //判断链表是否合法            System.out.println("index数据非法");            return null;        }        Node temp = head.next;        //遍历到要找的位置        for (int i = 0; i < length-index; i++) {                temp = temp.next;        }        return temp;    }

getLength(head)方法

/**     * 获取单链表的长度     * @param head 单链表的头节点     * @return 返回单链表的有效长度     */    public int getLength(Node head){           if (head.next == null){               return 0;        }        int length = 0;        Node temp = head.next;        while(temp != null){               length++;            temp = temp.next;        }        return length;    }

完整代码

package LinkedList.test;/** * @version 1.0 * @auther WangCode * @date 2021/4/3 16:08 */public class SingleLinkedListTest {       public static void main(String[] args) {           Node n1 = new Node(1, "123");        Node n2 = new Node(2, "23");        Node n3 = new Node(3, "3");        LinkedList linkedList = new LinkedList();        linkedList.add(n1);        linkedList.add(n2);        linkedList.add(n3);        Node listK = linkedList.getK(linkedList.getHead(), 1);        System.out.println(listK);    }}//定义一个节点class Node{       public int no;    public String data;    public Node next;    //构造方法    public Node(int no, String data){           this.no = no;        this.data = data;    }    @Override    public String toString() {           return "Node{" +                "no=" + no +                ", data='" + data + '\'' +                '}';    }}//创建单链表class LinkedList{       //初始化头节点    private Node head = new Node(0,"");    public Node getHead() {           return head;    }    /**     * 添加节点     * @param node     */    public void add(Node node){           Node temp = head;        while (true){               if (temp.next == null){   //找到链表最后一个节点                break;            }            temp = temp.next;        }        temp.next = node;//在链表的最后添加新的节点    }    /**     * 遍历链表     */    public void list(){           Node temp = head.next;        if (temp == null){               System.out.println("该链表为空");            return;        }        while(true){               if (temp == null){                   break;            }            System.out.println(temp);            temp = temp.next;        }    }    /**     * 获取单链表的长度     * @param head 单链表的头节点     * @return 返回单链表的有效长度     */    public int getLength(Node head){           if (head.next == null){               return 0;        }        int length = 0;        Node temp = head.next;        while(temp != null){               length++;            temp = temp.next;        }        return length;    }    /**     * 查找倒数第k个节点     * @param head 链表的头节点     * @param index  表示要查找结点的逆序下标     * @return 返回倒数第index个个点     */    public Node getK(Node head,int index){           if (head.next == null){               return null;        }        int length = getLength(head);        if (index <=0 || index > length){               System.out.println("index数据非法");            return null;        }        Node temp = head.next;        for (int i = 0; i < length-index; i++) {                temp = temp.next;        }        return temp;    }}

运行结果:

在这里插入图片描述

转载地址:http://xqqoz.baihongyu.com/

你可能感兴趣的文章
wxWidgets源码分析(9) - wxString
查看>>
[梁山好汉说IT] 梁山好汉和抢劫银行
查看>>
[源码解析] 消息队列 Kombu 之 基本架构
查看>>
[源码分析] 消息队列 Kombu 之 启动过程
查看>>
wx.NET CLI wrapper for wxWidgets
查看>>
Silverlight for linux 和 DLR(Dynamic Language Runtime)
查看>>
ASP.NET MVC Action Filters
查看>>
Powershell中禁止执行脚本解决办法
查看>>
OO_Unit2 多线程电梯总结
查看>>
git clone 出现fatal: unable to access ‘https://github 错误解决方法
查看>>
04_Mysql配置文件(重要参数)
查看>>
python 加密算法及其相关模块的学习(hashlib,RSA,random,string,math)
查看>>
JavaSE总结
查看>>
手动造轮子——基于.NetCore的RPC框架DotNetCoreRpc
查看>>
Python IO编程
查看>>
CSS入门总结
查看>>
使用 TortoiseGit 时,报 Access denied 错误
查看>>
基于 HTML5 WebGL 的污水处理厂泵站自控系统
查看>>
django-表单之模型表单渲染(六)
查看>>
c++之程序流程控制
查看>>