## Statement

• Difficulty: Easy
• Tag: `递归` `链表`

``````输入：head = [1,2,3,4,5]

``````

``````输入：head = [1,2]

``````

``````输入：head = []

``````

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

• Difficulty: Easy
• Tag: `Recursion` `Linked List`

Given the `head` of a singly linked list, reverse the list, and return the reversed list.

Example 1:

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

Example 2:

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

Example 3:

``````Input: head = []
Output: []
``````

Constraints:

• The number of nodes in the list is the range `[0, 5000]`.
• `-5000 <= Node.val <= 5000`

Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

## Solution

``````#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\n"
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall rbegin(a), rend(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
#define MP make_pair

using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using VLL = std::vector<ll>;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

template <typename T, typename S>
inline bool chmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}

template <typename T, typename S>
inline bool chmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}

#ifdef LOCAL
#include <debug.hpp>
#else
#define dbg(...)
#endif

#ifdef LOCAL

struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};

#endif

class Solution {
public:
ListNode *res = nullptr;

}

return res;
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````