zoukankan      html  css  js  c++  java
  • 206. Reverse Linked List

    一、题目

      1、审题

      

      2、分析

        分别采用递归、迭代的方式将链表进行翻转。

    二、解答

      1、思路:

        方法一、

          迭代: 采用两个指针 pre、cur

        // iteratively
        public ListNode reverseList(ListNode head) {
            
            if(head == null)
                return null;
            
            ListNode pre = null;
        ListNode cur = head; ListNode next = null; while(cur != null) { next = cur.next; cur.next = pre; pre = cur; cur = next; } return pre; }

      方法二、

      递归:

        public ListNode reverseList3(ListNode head) {
            return reverseListHelper(head, null);
        }
        private ListNode reverseListHelper(ListNode head, ListNode newHead) {
                
            if(head == null)
                return newHead;
            ListNode next = head.next;
            head.next = newHead;
            return reverseListHelper(next, head);
        }
  • 相关阅读:
    catalina_home与catalina_base
    log4j配置
    lsof
    定时任务-crontab
    access日志配置
    java常识
    mysql事务隔离级别与实现原理
    文件描述符设置
    gpio 預設值
    synchronous interrupt and asynchronous interrupt
  • 原文地址:https://www.cnblogs.com/skillking/p/9822443.html
Copyright © 2011-2022 走看看