Computer science note/LeetCode

Reverse Linked List

Leetcode problem description and solution for "Reverse Linked List".

LeetCode - The World's Leading Online Programming Learning Platform
Given the head of a singly linked list, reverse the list, and return the reversed list.
给定单向链表的 , head 反转列表,并返回反转列表。

Example 1: 示例 1:

Input: head = [1,2,3,4,5] Output: [5,4,3,2,1]

Example 2: 示例 2:

Input: head = [1,2] Output: [2,1]

Example 3: 例3:

Input: head = [] Output: []

[Constraints: 约束](<Constraints: 约束:

The number of nodes in the list is the range [0, 5000]. 列表中的节点数是范围 [0, 5000] 。 -5000 %3C= Node.val <= 5000

Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? 跟进:链表可以迭代或递归地反转。你能同时实现两者吗?>)