-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathReverseList.cs
91 lines (75 loc) · 2.2 KB
/
ReverseList.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
题目名称:
反转链表
题目描述:
输入一个链表,反转链表后,输出新链表的表头。
代码结构:
class Solution
{
public ListNode ReverseList(ListNode pHead)
{
// write code here
}
}
*/
using System;
namespace ReverseList {
public class ListNode
{
public int val;
public ListNode next;
public ListNode (int x)
{
val = x;
}
}
class Solution {
/// <summary>
/// 解法1
/// 基本思路:
/// 使用三个指针pHead, last, next
/// pHead记录当前节点,last记录上一个节点,next记录下一个节点
/// 首先使用next保存当前节点的下一个节点,然后将当前节点的下一个节点指向last,实现反转
/// </summary>
public ListNode ReverseList(ListNode pHead)
{
ListNode last = null, next = null;
while(pHead != null){
next = pHead.next;
pHead.next = last;
last = pHead;
pHead = next;
}
return last;
}
/// <summary>
/// 解法2,递归
/// 基本思路:
/// 通过不断递归,先从链表的尾节点开始反转
/// 然后利用递归的回溯实现按照从尾到头的顺序反转每个节点
/// </summary>
public ListNode ReverseList2(ListNode pHead)
{
if(pHead == null || pHead.next == null) return pHead;
ListNode node = ReverseList2(pHead.next);
pHead.next.next = pHead;
pHead.next = null;
return node;
}
public void Print(ListNode head){
while(head != null){
Console.WriteLine(head.val);
head = head.next;
}
}
public void Test() {
ListNode pHead = new ListNode(1);
pHead.next = new ListNode(2);
pHead.next.next = new ListNode(3);
pHead.next.next.next = new ListNode(4);
// pHead = null;
// Print(ReverseList(pHead));
Print(ReverseList2(pHead));
}
}
}