zoukankan      html  css  js  c++  java
  • java实现链表的反转

    摘要

    最近笔试一家公司,其中一道题目:如何用java实现链表的反转,如:{a,b,c,d}反转后变为:{d,c,b,a},只能操作一个链表。 当时不晓得怎么做。还以为使用栈来实现。图样图森破。
    public class Node 
    
    {
    
    
        private Node next;
        private String value;
        
        public Node(String value){
            this.value=value;
        }
        public Node getNext() {
            return next;
        }
        public void setNext(Node next) {
            this.next = next;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
        
        public static void main(String[] args) 
        {
            Node head=new Node("a");
            Node node1=new Node("b");
            Node node2=new Node("c");
            Node node3=new Node("d");
            //初始化链表
            head.setNext(node1);
            node1.setNext(node2);
            node2.setNext(node3);
            System.out.println("打印链表反转前:");
    
    
            reverse(head);
            //设置head的下一个元素为null,注意:此时head已经成为链表尾部元素。
            head.next=null;
            while(node3!=null){
                System.out.print(node3.getValue());
                node3=node3.getNext();
                if(node3!=null){
                    System.out.print("->");
                }
            }
        }
        
        /**
         * 利用迭代循环到链表最后一个元素,然后利用nextNode.setNext(head)把最后一个元素变为
         * 第一个元素。
         * 
         * @param head
         */
        public static void reverse(Node head){
            if(head!=null){
                Node nextNode=head.getNext();
                if(nextNode!=null){
                    reverse(nextNode);
                    nextNode.setNext(head);
                }
            }
            
        }
    }
  • 相关阅读:
    10-22 训练 T2 plate
    C语言I博客作业03
    c语言I博客作业02
    数学的数字
    javascript 递归
    WEB 动画的一些实现方式
    javascript 中Object一些高效的操作方法
    javascript 中Array一些高效的操作方法
    mac os 的一些命令
    javascript 继承
  • 原文地址:https://www.cnblogs.com/hd92/p/13554204.html
Copyright © 2011-2022 走看看