剑指 Offer - 15:反转链表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AnselLyy/article/details/84447092

题目描述

输入一个链表,反转链表后,输出新链表的表头

题目链接:https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

解题思路

思路1:借助栈

思路2:递归

思路3:链表原地反转

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode pre = null;
        ListNode next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
}

猜你喜欢

转载自blog.csdn.net/AnselLyy/article/details/84447092