/** * 剑指 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; } }
暂时想不出来