zoukankan      html  css  js  c++  java
  • leetcode — reverse-linked-list

    /**
     * Source : https://leetcode.com/problems/reverse-linked-list/
     *
     *
     * Reverse a singly linked list.
     *
     * click to show more hints.
     *
     * Hint:
     * A linked list can be reversed either iteratively or recursively. Could you implement both?
     */
    public class ReverseLinkedList {
    
        /**
         *
         * 反转单向链表
         *
         * 使用迭代的方法
         *
         * @param head
         * @return
         */
        public Node reverseByIterative (Node head) {
            Node pre = null;
            while (head != null) {
                Node next = head.next;
                head.next = pre;
                pre = head;
                head = next;
            }
            return pre;
    
        }
    
        /**
         * 使用递归
         *
         * @param head
         * @return
         */
        public Node reverseByRecursion (Node head, Node pre) {
            if (head == null) {
                return head;
            }
            if (head.next == null) {
                head.next = pre;
                return head;
            }
            Node next = head.next;
            head.next = pre;
            pre = head;
            return reverseByRecursion(next, pre);
        }
    
    
        private static class Node {
            int value;
            Node next;
    
            @Override
            public String toString() {
                return "Node{" +
                        "value=" + value +
                        ", next=" + (next == null ? "" : next.value) +
                        '}';
            }
        }
    
        private static void print (Node node) {
            while (node != null) {
                System.out.println(node);
                node = node.next;
            }
            System.out.println();
        }
    
        public Node createList (int[] arr) {
            if (arr.length == 0) {
                return null;
            }
            Node head = new Node();
            head.value = arr[0];
            Node pointer = head;
            for (int i = 1; i < arr.length; i++) {
                Node node = new Node();
                node.value = arr[i];
                pointer.next = node;
                pointer = pointer.next;
            }
            return head;
        }
    
        public static void main(String[] args) {
            ReverseLinkedList reverseLinkedList = new ReverseLinkedList();
    
            print(reverseLinkedList.reverseByIterative(reverseLinkedList.createList(new int[]{1,2,3,4})));
            print(reverseLinkedList.reverseByIterative(reverseLinkedList.createList(new int[]{})));
    
            print(reverseLinkedList.reverseByRecursion(reverseLinkedList.createList(new int[]{1,2,3,4}), null));
            print(reverseLinkedList.reverseByRecursion(reverseLinkedList.createList(new int[]{}), null));
        }
    
    }
    
  • 相关阅读:
    博客园20071027上海聚会
    上海招聘.NET(C#)程序员
    招人
    漂亮的后台WebUi框架(有源码下载)
    js插件库系列导航
    PrestoSQL(trinodb)源码分析 执行(下)
    Extjs4 (二)
    Struts2(1)简介
    css中的字体
    什么是REST架构
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7774920.html
Copyright © 2011-2022 走看看