-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path206.反转链表.cs
44 lines (38 loc) · 921 Bytes
/
206.反转链表.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* @lc app=leetcode.cn id=206 lang=csharp
*
* [206] 反转链表
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution
{
private ListNode RecursionReverseList(ListNode nowNode, ListNode prevNode)
{
if (nowNode == null)
return prevNode;
ListNode temp = nowNode.next;
nowNode.next = prevNode;
return RecursionReverseList(temp, nowNode);
}
// recursion
public ListNode ReverseList(ListNode head)
{
if (head == null || head.next == null)
return head;
ListNode temp = head.next;
head.next = null;
return RecursionReverseList(temp, head);
}
}
// @lc code=end