zoukankan      html  css  js  c++  java
  • leetcode206 反转链表 两种做法(循环,递归)

    反转链表 leetcode206

    方法1 循环

    public ListNode reverseList(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode now = head;
            while (now.next != null) {
                ListNode temp = now.next;
                now.next = now.next.next;
                temp.next = head;
                head = temp;
            }
            return head;
        }
    

    方法2 递归

    public ListNode reverseList2(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode newHead = reverseList2(head.next);
            ListNode now =newHead;
            while (now.next != null) {
                now = now.next;
            }
            now.next = head;
            head.next = null;
            return newHead;
        }
    
  • 相关阅读:
    jsoup使用选择器语法来查找元素
    获取MD5值
    MD5
    反射机制的实现代码
    struts
    spring
    Hibernate
    商品信息录入
    extjs
    EasyUI
  • 原文地址:https://www.cnblogs.com/wmxl/p/11291754.html
Copyright © 2011-2022 走看看