前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >单链表常见问题

单链表常见问题

作者头像
切图仔
发布2022-09-14 16:18:12
1630
发布2022-09-14 16:18:12
举报
文章被收录于专栏:生如夏花绚烂生如夏花绚烂

求单链表节点中的个数

代码语言:javascript
复制
 /**
     * 求单链表中节点的个数
     * @return
     */
    public int countNode(){
        int count = 0;
        UserNode temp = head.next;
        while(true){
            if(temp==null){
                break;
            }
            count++;
            temp = temp.next;
        }

        return count; //4

    }

查找单链表中的第k个节点

代码语言:javascript
复制
 /**
     * 查找单链表中第k个节点
     * @param int index 要查找的第k个节点
     * @return
     */
    public void findNode(int index){
        if(head.next==null){
            System.out.println("链表中没有数据");
            return;
        }
        UserNode temp = head.next;
        //初始索引
        int count = 0;
        //是否找到某元素表示
        boolean flag = false;
        while (true){
            //已经遍历到单链表结尾
            if(temp==null){
                break;
            }
            count++;
            if(count==index) {
                flag = true;
                System.out.printf("找到第%d个节点为", count);
                System.out.println(temp);
                break;
            }
            temp = temp.next;
        }
        if(!flag){
            System.out.println("没有找到");
        }

    }

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

关键 int 倒数第n个节点的顺数位置 = 链表中节点数 - n + 1

代码语言:javascript
复制
  /**
     * 查找单链表中第倒数k个节点
     * @param int index 要查找的倒数k个节点
     * @return
     */
    public void findLastNode(int index){
        int singleLength = countNode();//获取链表中的节点数量
        //倒数第n个节点的顺数位置
        int down =  singleLength - index + 1;
        UserNode temp = head.next;
        //初始索引
        int count = 0;
        //是否找到某元素表示
        boolean flag = false;
        while (temp!=null){
            //已经遍历到单链表结尾
            count++;
            if(count==down) {
                flag = true;
                System.out.printf("倒数%d个节点为", index);
                System.out.println(temp);
                break;
            }
            temp = temp.next;
        }
        if(!flag){
            System.out.println("没有找到");
        }


    }

逆序打印链表中的节点,要求不改变链表中的结构

**第一种方式,利用栈的结构的先进后出的特点 **

代码语言:javascript
复制
import java.util.Stack;
 /**
     * 从链表末尾还是打印单链表
     */
    public void showLastList(){
       if(head.next == null){
           System.out.println("链表为空");
           return;
       }
       //创建一个栈
        Stack<UserNode> stack = new Stack<>();
       UserNode temp = head.next;
       while (temp!=null){
           //入栈
           stack.push(temp);
           //后移
           temp = temp.next;
       }
       //出栈
        while(stack.size() > 0){
            System.out.println(stack.pop());
        }
    }

单链表反转

代码语言:javascript
复制
 /**
     * 链表反转
     */
    public void reverseList(){

        //链表为空,或者链表中只有一个节点
        if(head.next==null || head.next==null){
            return;
        }
        //如果不为空,那么至少有一个节点
        UserNode temp = head.next;
        UserNode next = null; //当前节点的下一个节点标识
        UserNode newHead = new UserNode(0,"","");

        while (temp!=null){
            next = temp.next; //记录当前节点的下一个节点标识
            temp.next = newHead.next; //将temp的下一个节点指向链表最前端
            newHead.next = temp;
            //后移
            temp = next;
        }
        //将head.nex域指向newHead.next .实现单链表反转
        head.next = newHead.next;
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-01-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 求单链表节点中的个数
  • 查找单链表中的第k个节点
  • 查找单链表倒数第n个节点
  • 逆序打印链表中的节点,要求不改变链表中的结构
  • 单链表反转
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档