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);
        }
  • 相关阅读:
    LaTeX 超链接
    剑指offer2 数组
    LaTeX 插入源代码
    RGB
    linux 程序在后台运行
    Linux Vim编辑与退出
    复杂度估计
    剑指offer 2 loading...
    剑指offer2 整数
    剑指offer2 字符串
  • 原文地址:https://www.cnblogs.com/skillking/p/9822443.html
Copyright © 2011-2022 走看看