• 链表--反转单向和双向链表


    剑指offer——反转链表

    反转单向链表

    要求:如果链表长度为N, 时间复杂度要求为O(N), 额外空间复杂度要求为O(1)

    需要有一个前向指针,和一个后向指针,分别指向当前节点反转之后的next结点,和当前节点当前的next结点

     

    public static ListNode reverse(ListNode head){
            if(head == null) return null;
            ListNode preNode = null;
            while(head != null){
                ListNode nextNode = head.next;
                head.next = preNode;
                preNode = head;
                head = nextNode;
            }
    
            return preNode;
        }
    

      

    反转双向链表

    解:准备两个结点preNode和nextNode,

    用nextNode记录当前结点的next

    然后当前结点的next指向preNode

    当前结点的pre指向nextNode

    preNode指向当前结点

    当前结点再指向nextNode

        public static DoubleNode reverseDoubleNode(DoubleNode head){
            if(head == null) return null;
            DoubleNode preNode = null;
    
            while(head != null){
                DoubleNode nextNode = head.next;
                head.next = preNode;
                head.pre = nextNode;
                preNode = head;
                head = nextNode;
            }
            return preNode;
        }
    

      

    最终代码

    package LinkQ;
    
    /**
     * Created by Skye on 2018/4/8.
     */
    public class ReverseLinkedList {
    
        //单向链表
        public static class ListNode{
            public int val;
            public ListNode next = null;
    
            public ListNode(int val) {
                this.val = val;
            }
        }
    
        public static ListNode reverse(ListNode head){
            if(head == null) return null;
            ListNode preNode = null;
            while(head != null){
                ListNode nextNode = head.next;
                head.next = preNode;
                preNode = head;
                head = nextNode;
            }
    
            return preNode;
        }
    
        public static void print(ListNode head){
            ListNode n = head;
            while(n != null){
                System.out.print(n.val + " ");
                n = n.next;
            }
            System.out.println();
        }
    
    
    
        //双向链表
        public static class DoubleNode{
            public int val;
            public DoubleNode pre = null;
            public DoubleNode next = null;
    
            public DoubleNode(int val){
                this.val = val;
            }
        }
    
        public static DoubleNode reverseDoubleNode(DoubleNode head){
            if(head == null) return null;
            DoubleNode preNode = null;
    
            while(head != null){
                DoubleNode nextNode = head.next;
                head.next = preNode;
                head.pre = nextNode;
                preNode = head;
                head = nextNode;
            }
            return preNode;
        }
    
        public static void print(DoubleNode head){
            DoubleNode end = null;
            while(head != null){
                System.out.print(head.val + " ");
                end = head;
                head = head.next;
            }
            System.out.println();
            while(end != null){
                System.out.print(end.val + " ");
                end = end.pre;
            }
            System.out.println();
        }
    
        public static void main(String[] args){
            ListNode node = new ListNode(0);
            ListNode head = node;
            for(int i = 1; i < 10; i++){
                node.next = new ListNode(i);
                node = node.next;
            }
            node.next = null;
            print(head);
            print(reverse( head ));
    
            DoubleNode node1 = new DoubleNode( 0 );
            node1.pre = null;
            DoubleNode head1 = node1;
            for(int i = 1; i < 10; i++){
                node1.next = new DoubleNode( i );
                DoubleNode preNode = node1;
                node1 = node1.next;
                node1.pre = preNode;
            }
            node1.next = null;
            print(head1);
            print(reverseDoubleNode(head1));
        }
    }
    

      

  • 相关阅读:
    02.ZooKeeper的Java客户端使用
    01.ZooKeeper安装和介绍
    02.Elasticsearch入门
    01.Elasticsearch安装
    01.ActiveMQ安装部署
    springboot项目打包时提示“程序包xxx不存在,找不到符号”
    Eclipse提交git代码 报错authentication not supported
    Eclipse提交git代码 报错authentication not supported
    utf8mb4_general_ci报错解决方案
    mysql开启远程访问
  • 原文地址:https://www.cnblogs.com/SkyeAngel/p/8735433.html
Copyright © 2020-2023  润新知