## Algorithm

Problem Name: 206. Reverse 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`

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode *a, *b, *c;

a = NULL;
while (b) {
c = b->next;
b->next = a;
a = b;
b = c;
}
​
return a;
}
``````
Copy The Code &

Input

cmd

Output

cmd
[5,4,3,2,1]

### #2 Code Example with C++ Programming

```Code - C++ Programming```

``````
// Iterative
class Solution {
public:
while(cur){
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
};

// Recursive
class Solution {
public:
return node;
}
};
``````
Copy The Code &

Input

cmd

Output

cmd
[5,4,3,2,1]

### #3 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
ListNode prev = null;
ListNode next = null;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
}
``````
Copy The Code &

Input

cmd

Output

cmd
[2,1]

### #4 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const pre = new ListNode(null, head)
while(cur.next) {
let tmp = pre.next
pre.next = cur.next
cur.next = cur.next.next
pre.next.next = tmp
}

return pre.next
};
``````
Copy The Code &

Input

cmd

Output

cmd
[2,1]

### #5 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def reverseList(self, head: ListNode, pre = None) -> ListNode:
``````
Copy The Code &

Input

cmd

Output

cmd
[]

### #6 Code Example with C# Programming

```Code - C# Programming```

``````
namespace LeetCode
{
{
{

while (current != null)
{
var next = current.next;

current = next;
}

}
}
}
``````
Copy The Code &

Input

cmd