栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 单链表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 单链表

public class linked {

    private Note first;

    public void add(T t) {
        if (first == null) {
            first = new Note<>(t);
            return;
        }
        Note note = first;
        while (note != null) {
            if (note.next == null) {
                note.next = new Note<>(t);
                return;
            } else {
                note = note.next;
            }
        }
    }

    public Note getHead() {
        return first;
    }

    public T getT(Note note) {
        return note.t;
    }

    
    public void reversal() {
        //没数据或者只有一个数据的直接返回
        if (first == null || first.next == null) return;
        Note preNote = null;
        Note curNote = first;
        Note tem;//记录下次循环的开头节点
        while (curNote != null) {
            tem = curNote.next;
            curNote.next = preNote;
            preNote = curNote;
            curNote = tem;
        }
        first = preNote;
    }

    
    public void reversal(Note head) {
        if (head == null) return;
        if (head.next == null) {
            first = head;
            return;//可以返回最后节点,变成链表新的头节点
        }

        Note next = head.next;
        reversal(next);
        next.next = head;
        head.next = null;
    }

    public void printf() {
        Note note = this.first;
        while (note != null) {
            System.out.println(note.getT().toString());
            note = note.next;
        }
    }

    private static class Note {

        private E t;
        private Note next;

        public E getT() {
            return t;
        }

        public Note(E t) {
            this.t = t;
        }

        public Note getNext() {
            return next;
        }

    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/389763.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号