-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReverseLinkedList.cs
63 lines (56 loc) · 1.91 KB
/
ReverseLinkedList.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
//Problema de leet code
//https://leetcode.com/problems/reverse-linked-list/description/
//Se trata de revertir una lista enlazada
//input: 1->-2->3
//output 3->2->1
//El enfoqué que use fue leer la lista de entrada y almacenar los numeros en un Stack (pila)
//Después, extraer los números del Stack e irlos agregando en una segunda lista enlazada hasta vaciarla.
//Recuerda que el Stack es un LastInFirstOut. Último en entrar, primero en salir de modo que obtenemos los numeros en el orden esperado sin necesidad de un loop.
/**
* 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) {
//let's try the following approach
//read first the linked list
//store the numbers using a stack
//then pop the items from the stack and add them to a new linked list
Stack<int> stack = new Stack<int>();
var currentNode = head;
while(true)
{
if(currentNode == null) break;
else
{
stack.Push(currentNode.val);
currentNode = currentNode.next;
}
}
if(stack.Count == 0) return null;
head = null;
currentNode = null;
while(stack.Count > 0)
{
if(head == null) head = new ListNode(stack.Pop(), null);
else if(head.next == null) {
currentNode = new ListNode(stack.Pop(), null);
head.next = currentNode;
}
else
{
var next = new ListNode(stack.Pop(), null);
currentNode.next = next;
currentNode = next;
}
}
return head;
}
}