【刷题 issue14】程序员代码面试指南 —— IT 名企算法与数据结构题目最优解

第二章 链表问题

2.4 反转单向和双向链表

【题目】

分别实现反转单向链表和反转双向填表的函数。

【要求】

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

【难度】

士 ★☆☆☆

【题解】

本题比较简单,注意在反转链表之后更新链表新的头节点。

【实现】

  • ReverseSingleLinkedList.java
public class ReverseSingleLinkedList {

    private static class Node {
        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
        }
    }

    private Node head;

    public ReverseSingleLinkedList(int[] arr) {
        buildSingleLinkedList(arr);
    }

    private void buildSingleLinkedList(int[] arr) {
        if (arr != null && arr.length != 0) {
            Node preNode = this.head = new Node(arr[0]);
            for (int i = 1; i < arr.length; i++) {
                preNode = preNode.next = new Node(arr[i]);
            }
        }
    }

    public void reverseSingleLinkedList() {
        if (this.head == null || this.head.next == null) {
            return;
        }
        Node preNode = null;
        Node curNode = null;
        while (this.head != null) {
            curNode = this.head.next;
            this.head.next = preNode;
            preNode = this.head;
            this.head = curNode;
        }
        this.head = preNode;
        // Node pre = null;
        // Node cur = head;
        // Node post = head.next;
        // while (post != null) {
        //     cur.next = pre;
        //     pre = cur;
        //     cur = post;
        //    post = post.next;
        // }
        // cur.next = pre;
        // head = cur;
    }

}

  • ReverseDoubleLinkedList.java
public class ReverseDoubleLinkedList {

    private static class Node {
        public int value;

        public Node last;
        public Node next;

        public Node(int value) {
            this.value = value;
        }
    }

    private Node head;

    public ReverseDoubleLinkedList(int[] arr) {
        buildDoubleLinkedList(arr);
    }

    private void buildDoubleLinkedList(int[] arr) {
        if (arr != null && arr.length != 0) {
            Node preNode = this.head = new Node(arr[0]);
            for (int i = 1; i < arr.length; i++) {
                preNode.next = new Node(arr[i]);
                preNode.next.last = preNode;
                preNode = preNode.next;
            }
        }
    }

    public void reverseDoubleLinkedList() {
        if (this.head == null || this.head.next == null) {
            return;
        }
        Node preNode = null;
        Node curNode = null;
        while (this.head != null) {
            curNode = this.head.next;
            this.head.next = preNode;
            this.head.last = curNode;
            preNode = this.head;
            this.head = curNode;
        }
        this.head = preNode;
        // Node cur = head;
        // Node post = head.next;
        // while (post != null) {
        //     cur.next = cur.last;
        //     cur.last = post;
        //     cur = post;
        //     post = post.next;
        // }
        // head = cur;
    }

}
  • Test.java
public class Test {

    private ReverseSingleLinkedList singleLinkedList;
    private ReverseDoubleLinkedList doubleLinkedList;

    public Test(int[] arr) {
        this.singleLinkedList = new ReverseSingleLinkedList(arr);
        this.doubleLinkedList = new ReverseDoubleLinkedList(arr);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        Test test = new Test(arr);
        test.singleLinkedList.reverseSingleLinkedList();
        test.doubleLinkedList.reverseDoubleLinkedList();
    }

}
发布了147 篇原创文章 · 获赞 72 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Pranuts_/article/details/100171553