Skip to content
💻 在线刷题 · 全屏 IDE 模式进入刷题模式 →

206. 反转链表

题目描述

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

 

示例 1:

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

示例 2:

输入:head = [1,2]
输出:[2,1]

示例 3:

输入:head = []
输出:[]

 

提示:

  • 链表中节点的数目范围是 [0, 5000]
  • -5000 <= Node.val <= 5000

 

进阶:链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?

方法一:双指针

只需要改变链表的next指针的指向,直接将链表反转即可,时间复杂度 O(n),空间复杂度 O(n)。其中 n 为链表的长度。

图示如下:

img

可视化演示

head = [1, 2, 3, 4, 5] 为例,演示双指针反转:prev 为已反转的链表,curr 为剩余待反转链表,每轮把 curr 头节点摘下接到 prev 之前。蓝色为刚反转的节点,黄色为下一个待处理节点。点击 ▶ 播放,或逐步操作。

prev
(空链表)
curr
21
32
43
54
当前操作比较/参照已完成已连接目标/结果空节点

初始化:prev = null,curr = head = 1。head = [1,2,3,4,5]。

1 / 7

java
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return head;
        }

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

        head = prev;
        return head;
    }
}
ts
class Solution {
    reverseList(head: ListNode | null): ListNode | null {
        if (head === null) {
            return head;
        }

        let prev: ListNode | null = null;
        let curr: ListNode | null = head;
        while (curr !== null) {
            const next: ListNode | null = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }

        return prev;
    }
}

方法二:头插法

创建虚拟头节点 dummy,遍历链表,将每个节点依次插入 dummy 的下一个节点。遍历结束,返回 dummy.next

时间复杂度 O(n),空间复杂度 O(1)。其中 n 为链表的长度。

图示如下:

image-20250424115032080

可视化演示

head = [1, 2, 3, 4, 5] 为例,演示头插法反转:dummy 为虚拟头节点,每轮把 curr 的头节点摘下插入 dummy 之后,最终 dummy.next 即为反转后的链表。蓝色为刚插入的节点。点击 ▶ 播放,或逐步操作。

curr
21
32
43
54
result
当前操作比较/参照已完成已连接目标/结果空节点

初始化:dummy = new ListNode(),curr = head = 1。head = [1,2,3,4,5]。

1 / 7

java
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode dummy = new ListNode();
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = dummy.next;
            dummy.next = curr;
            curr = next;
        }
        return dummy.next;
    }
}
cpp
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* dummy = new ListNode();
        ListNode* curr = head;
        while (curr) {
            ListNode* next = curr->next;
            curr->next = dummy->next;
            dummy->next = curr;
            curr = next;
        }
        return dummy->next;
    }
};
ts
function reverseList(head: ListNode | null): ListNode | null {
    if (head == null) {
        return head;
    }
    let pre = null;
    let cur = head;
    while (cur != null) {
        const next = cur.next;
        cur.next = pre;
        [pre, cur] = [cur, next];
    }
    return pre;
}

方法三:从前向后递归

时间复杂度 O(n),空间复杂度 O(n)。其中 n 为链表的长度。

可视化演示

head = [1, 2, 3, 4, 5] 为例,演示从前向后递归反转:递归函数 rescursion(prev, curr) 思路与双指针法一致,每层把 curr 头节点接到 prev 之前,再递归处理剩余部分。蓝色为刚反转的节点,黄色为下一个待处理节点。点击 ▶ 播放,或逐步操作。

prev
(空链表)
curr
21
32
43
54
当前操作比较/参照已完成已连接目标/结果空节点

调用 rescursion(null, head):prev = null,curr = head = 1。

1 / 7

java
class Solution {
    public ListNode reverseList(ListNode head) {
        return rescursion(null, head);
    }

    private ListNode rescursion(ListNode prev, ListNode curr) {
        if (curr == null) {
            return prev;
        }
        ListNode next = curr.next;
        curr.next = prev;

        return rescursion(curr, next);
    }
}

方法四:从后向前递归

递归反转链表的第二个节点到尾部的所有节点,然后 head 插在反转后的链表的尾部。

时间复杂度 O(n),空间复杂度 O(n)。其中 n 为链表的长度。

可视化演示

head = [1, 2, 3, 4, 5] 为例,演示从后向前递归反转:先递归反转 head.next 得到 ans,回溯时再把 head 节点接到 ans 末尾。蓝色为当前正在接回的节点,绿色为已反转部分。点击 ▶ 播放,或逐步操作。

head
21
32
43
54
ans
(空链表)
当前操作比较/参照已完成已连接目标/结果空节点

reverseList([1,2,3,4,5]):head=1,head.next=2 非空,递归 reverseList(head.next)=reverseList([2,3,4,5])。

1 / 10

java
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
         // 递归调用,翻转第二个节点开始往后的链表
        ListNode ans = reverseList(head.next);
        // 翻转头节点与第二个节点的指向
        head.next.next = head;
        //此时的 head 节点为尾节点,next 需要指向 NULL
        head.next = null;
        return ans;
    }
}
cpp
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (!head || !head->next) return head;
        ListNode* ans = reverseList(head->next);
        head->next->next = head;
        head->next = nullptr;
        return ans;
    }
};
ts
const rev = (pre: ListNode | null, cur: ListNode | null): ListNode | null => {
    if (cur == null) {
        return pre;
    }
    const next = cur.next;
    cur.next = pre;
    return rev(cur, next);
};

function reverseList(head: ListNode | null): ListNode | null {
    if (head == null) {
        return head;
    }
    const next = head.next;
    head.next = null;
    return rev(head, next);
}
python
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if head is None or head.next is None:
            return head
        ans = self.reverseList(head.next)
        head.next.next = head
        head.next = None
        return ans

方法五:使用栈

时间复杂度 O(n),空间复杂度 O(n)

可视化演示

head = [1, 2, 3, 4, 5] 为例,演示用栈反转:先把所有节点依次入栈,再依次弹出并重建新链表。top 指向栈顶,newHead 为新链表头,current 指向已重建的末尾。蓝色为当前弹出的节点,红色为最终结果。点击 ▶ 播放,或逐步操作。

current
21
32
43
54
stack
(空链表)
当前操作比较/参照已完成已连接目标/结果空节点

初始化:stack = new Stack<>(),current = head = 1。开始遍历链表,依次入栈。

1 / 8

java
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        Stack<ListNode> stack = new Stack<>();
        ListNode current = head;
        while (current != null) {
            stack.push(current);
            current = current.next;
        }
        ListNode newHead = stack.pop();
        current = newHead;
        
        while (!stack.isEmpty()) {
            ListNode node = stack.pop();
            current.next = node;
            current = node;
        }
       
        current.next = null;
        return newHead;
    }
}
ts
class Solution {
    reverseList(head: ListNode | null): ListNode | null {
        if (head === null || head.next === null) {
            return head;
        }

        const stack: ListNode[] = [];
        
        let current: ListNode | null = head;
        while (current !== null) {
            stack.push(current);
            current = current.next;
        }
        const newHead: ListNode = stack.pop()!;
        current = newHead;
        
        while (stack.length > 0) {
            const node = stack.pop()!;
            current.next = node;
            current = node;
        }
        
        current.next = null;
        return newHead;
    }
}

Released under the MIT License.