【剑指Offer 24】反转链表

双指针

/**
 * 剑指 Offer 24. 反转链表
 * https://leetcode.cn/problems/fan-zhuan-lian-biao-lcof/
 * 
 * 思路:双指针
 * */
public class Solution1 {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode prev = null;
        ListNode next = head;
        while (next != null) {
            ListNode temp = next.next;
            next.next = prev;
            prev = next;
            next = temp;
        }
        return prev;
    }
}

递归/栈

暂时想不出来

posted @ 2022-06-26 23:08  廖子博  阅读(18)  评论(0编辑  收藏  举报