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);
        }
  • 相关阅读:
    hdu 4144 状态压缩dp
    hdu 4118 树形dp
    hdu 4115 2-SAT判定
    hdu 4085 斯坦纳树
    hdu 3311 斯坦纳树
    hdu 4081 最小生成树+树形dp
    hdu 4424 并查集
    洛谷P2661信息传递
    洛谷P2746校园网
    树状数组模板
  • 原文地址:https://www.cnblogs.com/skillking/p/9822443.html
Copyright © 2011-2022 走看看