【剑指Offer 24】反转链表

2022/6/27 6:23:44

本文主要是介绍【剑指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;
    }
}

递归/栈

暂时想不出来



这篇关于【剑指Offer 24】反转链表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程