• 链表反转问题


    在面试过程中,很有可能面试官让手写一个链表反转程序,对于链表反转,实现的有两种方式:

    第一种是普通方式:即遍历调整,先将当前节点下一个节点缓存之后,然后再调整当前节点的位置

    第二种方式:递归调用,即在调用当前节点时,先处理后续节点

    class Node {  
        //变量  
        private int record;  
        //指向下一个对象  
        private Node nextNode;  
      
        public Node(int record) {  
            super();  
            this.record = record;  
        }  
        public int getRecord() {  
            return record;  
        }  
        public void setRecord(int record) {  
            this.record = record;  
        }  
        public Node getNextNode() {  
            return nextNode;  
        }  
        public void setNextNode(Node nextNode) {  
            this.nextNode = nextNode;  
        }  
    }  
      
    /** 
     * @author luochengcheng 
     *  两种方式实现单链表的反转(递归、普通) 
     *  新手强烈建议旁边拿着纸和笔跟着代码画图(便于理解) 
     */  
    public class ReverseSingleList {  
        /**  
         * 递归,在反转当前节点之前先反转后续节点  
         */  
        public static Node reverse(Node head) {  
            if (null == head || null == head.getNextNode()) {  
                return head;  
            }  
            Node reversedHead = reverse(head.getNextNode());  
            head.getNextNode().setNextNode(head);  
            head.setNextNode(null);  
            return reversedHead;  
        }  
      
        /**  
         * 遍历,将当前节点的下一个节点缓存后更改当前节点指针  
         *   
         */  
        public static Node reverse2(Node head) {  
            if (null == head) {  
                return head;  
            }  
            Node pre = head;  
            Node cur = head.getNextNode();  
            Node next;  
            while (null != cur) {  
                next = cur.getNextNode();  
                cur.setNextNode(pre);  
                pre = cur;  
                cur = next;  
            }  
            //将原链表的头节点的下一个节点置为null,再将反转后的头节点赋给head     
            head.setNextNode(null);  
            head = pre;  
              
            return head;  
        }  
      
        public static void main(String[] args) {  
           //头结点是为了操作的统一与方便而设立的,放在第一个元素结点之前,其数据域一般无意义
            Node head = new Node(0);  
            Node tmp = null;  
            Node cur = null;  
            // 构造一个长度为10的链表,保存头节点对象head     
            for (int i = 1; i < 10; i++) {  
                tmp = new Node(i);  
                if (1 == i) {  
                    head.setNextNode(tmp);  
                } else {  
                    cur.setNextNode(tmp);  
                }  
                cur = tmp;  
            }  
            //打印反转前的链表  
            Node h = head;  
            while (null != h) {  
                System.out.print(h.getRecord() + " ");  
                h = h.getNextNode();  
            }  
            //调用反转方法 
            //第一种方法是递归调用
            head=reverse(head);
            //第二种方法是普通调用
            //head = reverse2(head);  
            
            System.out.println("
    **************************");  
            //打印反转后的结果  
            while (null != head) {  
                System.out.print(head.getRecord() + " ");  
                head = head.getNextNode();  
            }  
        }  
    }  
     
    View Code
  • 相关阅读:
    004-spring cache-声明性的基于XML的缓存
    003-spring cache-JCache (JSR-107) annotations
    002-spring cache 基于注解的缓存-02详细-Cacheable 、CachePut、CacheEvict、Caching、CacheConfig、EnableCaching、自定义
    002-spring cache 基于注解的缓存-01-关键注解概述、spel、缓存Key 与 缓存解析器
    001-springboot cache 简介、基础使用
    tools-eclipse-004-UML图安装
    001-Spring的设计理念和整体架构
    java-信息安全(十八)java加密解密,签名等总结
    005-java的Annotation
    002-原始jpa以及基本加载过程,基本sql使用
  • 原文地址:https://www.cnblogs.com/peizhe123/p/4885339.html
Copyright © 2020-2023  润新知