Leetcode——206. 反转链表(Java)

在这里插入图片描述
递归方法如下:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;
          ListNode H   = reverseList(head.next);
          head.next.next    = head;
          head.next         = null;
          return H;
    }
}

其中H其实是一个新的指针,而不是新的链表,始终只有一个链表
理解图示
在这里插入图片描述
在这里插入图片描述

发布了46 篇原创文章 · 获赞 3 · 访问量 7644

猜你喜欢

转载自blog.csdn.net/qq_43604667/article/details/90051360