ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-04-03 19:05:45  阅读:118  来源: 互联网

标签:Node head 单链 return temp next public 表中 倒数第


在这里插入图片描述

问题:

查找单链表倒数第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;
    }
}

运行结果:
在这里插入图片描述

标签:Node,head,单链,return,temp,next,public,表中,倒数第
来源: https://blog.csdn.net/qq_43549291/article/details/115418058

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有