• 206. Reverse Linked List


    原题链接:https://leetcode.com/problems/reverse-linked-list/description/
    题目本身不难,难的是怎么写出高效优雅的代码来:

    import java.util.Stack;
    
    /**
     * Created by clearbug on 2018/2/26.
     */
    public class Solution {
    
        public static void main(String[] args) {
            Solution s = new Solution();
            ListNode first = new ListNode(1);
            ListNode second = new ListNode(2);
            ListNode third = new ListNode(3);
    
            first.next = second;
            second.next = third;
    
            ListNode tail = s.reverseList(first);
            System.out.println(tail);
        }
    
        /**
         * 官方方法二:递归实现,比我的要简介很多
         *
         * Submission Detail: beats 25.36%
         * Runtime: 1 ms
         *
         * @param head
         * @return
         */
        public ListNode reverseList(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
    
            ListNode tail = reverseList(head.next);
            head.next.next = head;
            head.next = null;
            return tail;
        }
    
        /**
         * 官方方法一:迭代实现,原来只需要两个指针即可,根本不需要一个栈
         *
         * Submission Detail: beats 100%
         * Runtime: 0 ms
         *
         * @param head
         * @return
         */
        public ListNode reverseList3(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
    
            ListNode prev = null, curr = head;
            while (curr != null) {
                ListNode nextNode = curr.next;
                curr.next = prev;
                prev = curr;
                curr = nextNode;
            }
    
            return prev;
        }
    
        // 从提交结果来看,我的两种方法效率都很差,那么就去看看官方答案吧!
    
        /**
         * 我的方法二:迭代实现
         *
         * Submission Detail: beats 2.93%
         * Runtime: 3 ms
         *
         * @param head
         * @return
         */
        public ListNode reverseList2(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
    
            Stack<ListNode> stack = new Stack<>();
            while (head != null) {
                stack.push(head);
                head = head.next;
            }
    
            ListNode tail = stack.pop();
            ListNode temp = tail;
            while (!stack.empty()) {
                ListNode node = stack.pop();
                node.next = null;
                temp.next = node;
                temp = temp.next;
            }
            return tail;
        }
    
        /**
         * 我的方法一:递归实现
         *
         * Submission Detail: beats 1.19%
         * Runtime: 27 ms
         *
         * @param head
         * @return
         */
        public ListNode reverseList1(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
    
            ListNode tail = reverseList(head.next);
            ListNode temp = tail;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = head;
            head.next = null;
            return tail;
        }
    }
    
  • 相关阅读:
    03 JVM 从入门到实战 | 简述垃圾回收算法
    02 JVM 从入门到实战 | 什么样的对象需要被 GC
    01 JVM 从入门到实战 | 什么是 JVM
    从一道面试题探究 Integer 的实现
    程序员如何写一份更好的简历
    自己动手实现分布式任务调度框架(续)
    一个excel(20M)就能干趴你的poi,你信吗?
    一个普通类就能干趴你的springboot,你信吗?
    自己动手实现springboot配置(非)中心
    自己动手实现分布式任务调度框架
  • 原文地址:https://www.cnblogs.com/optor/p/8707769.html
Copyright © 2020-2023  润新知