欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

自定义单链表并通过递归实现链表反转

程序员文章站 2022-03-03 12:21:00
自定义单链表并通过递归实现链表反转自定义单向链表public class MyLinkedList { private Node first; private Node last; private int size; public Node getFirst() { return first; } public Node getLast() { return last; } public void...

自定义单链表并通过递归实现链表反转

  • 自定义单向链表

    public class MyLinkedList {
    
        private Node first;
        private Node last;
        private int size;
    
        public Node getFirst() {
            return first;
        }
    
        public Node getLast() {
            return last;
        }
    
        public void add(Object obj) {
            Node node = new Node(obj, null);
            if (first == null) {
                first = node;
            } else {
                last.setNext(node);
            }
            last = node;
            size++;
        }
    
        public int size() {
            return size;
        }
    
        public void print() {
            if (size == 0) {
                System.out.println("该链表还没有存放数据!");
            }
            Node node = first;
            while (node != null) {
                System.out.print(node.getElement() + "\t");
                node = node.getNext();
            }
        }
    }
    
  • 自定义节点

    public class Node {
    
        private Object element;
        private Node next;
    
        public Node() {
        }
    
        public Object getElement() {
            return element;
        }
    
        public void setElement(Object element) {
            this.element = element;
        }
    
        public Node getNext() {
            return next;
        }
    
        public void setNext(Node next) {
            this.next = next;
        }
    
        public Node(Object obj, Node next) {
            this.element = obj;
            this.next = next;
        }
    
    }
    
  • 通过递归实现反转

    public class Test {
    
        public static void main(String[] args) {
            MyLinkedList myLinkedList = new MyLinkedList();
            for (int i = 0; i < 10; i++) {
                myLinkedList.add("节点" + i);
            }
            MyLinkedList myLinkedList1 = new MyLinkedList();
            System.out.println("原链表:");
            myLinkedList.print();
            System.out.println("\n转换后的链表");
            MyLinkedList test = test(myLinkedList, myLinkedList1, myLinkedList.size() - 1);
            test.print();
        }
    
        public static MyLinkedList test(MyLinkedList myLinkedList, MyLinkedList myLinkedList1, int size) {
            if (size >= 1) {
                Node node = myLinkedList.getFirst();
                for (int i = size; i >= 1; i--) {
                    node = node.getNext();
                }
                myLinkedList1.add(node.getElement());
                test(myLinkedList, myLinkedList1, --size);
            } else {
                myLinkedList1.add(myLinkedList.getFirst().getElement());
            }
            return myLinkedList1;
        }
    }
    

运行截图:

自定义单链表并通过递归实现链表反转

本文地址:https://blog.csdn.net/Russell_M/article/details/107376714

相关标签: 单链表