-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC#.cs
49 lines (46 loc) · 1.39 KB
/
C#.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
/**
* 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 {
public ListNode ReverseList(ListNode head) {
if (head == null || head.next == null) return head;
//return Iterative(head);
return Recursive(null, head);
}
private ListNode Iterative(ListNode head){
ListNode prev = null;
var curr = head;
// store temp copy of the next node,
// update current to |point| to previous
// update previous to be the current node
// update next to be curr, and repeat
while (curr != null){
var next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
// once curr is null, end of list reached,
// return prev as head of new reversed list.
return prev;
}
private ListNode Recursive(ListNode prev, ListNode curr){
// if current node is null, end of list
// return prev as head of newly reversed list
if (curr == null) return prev;
// temp store the next node
var next = curr.next;
// point curr node to prev
curr.next = prev;
// repeat with next set of nodes, curr as prev, next as curr.
return Recursive(curr, next);
}
}