TypechoJoeTheme

Toasobi的博客

反转链表(简单)

本文最后更新于2023年03月14日,已超过555天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

这道题主要考察递归做法。在本题递归思路中,采用模拟(纸上求解)的方式得出递归的实现

代码如下:

<div>class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode node = reverseList(head.next);
        head.next.next = head; //如果此处head是4,则表示在4,5之间加上5指向4的指针
        head.next = null; //此处则表示删去4指向5的指针
        return node;
    }
}</div>
朗读
赞(0)
评论 (0)