# C#LeetCode刷题之#206-反转链表（Reverse Linked List）

Reverse a singly linked list.

Input: 1->2->3->4->5->NULL

Output: 5->4->3->2->1->NULL

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

```public class Program {

public static void Main(string[] args) {
var head = new ListNode(1) {
next = new ListNode(2) {
next = new ListNode(3) {
next = new ListNode(4) {
next = new ListNode(5)
}
}
}
};

var res = ReverseList(head);
ShowArray(res);

head = new ListNode(10) {
next = new ListNode(7) {
next = new ListNode(23) {
next = new ListNode(86) {
next = new ListNode(56)
}
}
}
};

ShowArray(res);

}

private static void ShowArray(ListNode list) {
var node = list;
while(node != null) {
Console.Write(\$"{node.val} ");
node = node.next;
}
Console.WriteLine();
}

private static ListNode ReverseList(ListNode head) {
var node = head;
ListNode pre = null;
while(node != null) {
var temp = node.next;
node.next = pre;
pre = node;
node = temp;
}
return pre;
}

private static ListNode ReverseList2(ListNode head) {
var result = ReverseList2(head.next);
return result;
}

public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}

}```

```5 4 3 2 1
56 86 23 7 10```