-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.cs
197 lines (160 loc) · 4.67 KB
/
LinkedList.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
namespace LeetCodeSolutions.LinkedList
{
public class Node
{
public Node(int value)
{
this.Value = value;
}
public int Value { get; }
public Node NextNode { get; set; }
}
public class LinkedList
{
private Node Head { get; set; }
private int? nodesCount;
public Node GetNode(int index)
{
if (nodesCount < 0 || Head == null || index < 0 || index > nodesCount)
return null;
Node currentNode = Head;
for (int i = 0; i < index; i++)
{
currentNode = currentNode.NextNode;
}
return currentNode;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int Get(int index)
{
var node = GetNode(index);
if (node == null)
return -1;
return node.Value;
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
public void AddAtHead(int val)
{
Node newHead = new Node(val);
newHead.NextNode = Head;
Head = newHead;
nodesCount++;
}
/** Append a node of value val to the last element of the linked list. */
public void AddAtTail(int val)
{
if (Head == null)
{
AddAtHead(val);
}
Node current = Head;
while (current.NextNode != null)
{
current = current.NextNode;
}
current.NextNode = new Node(val);
nodesCount++;
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
public void AddAtIndex(int index, int val)
{
if (index > nodesCount || index < 0)
{
return;
}
if (index == nodesCount)
{
AddAtTail(val);
return;
}
Node current = GetNode(index);
Node prev = GetNode(index - 1);
if (prev == null)
{
AddAtHead(val);
return;
}
Node newNode = new Node(val);
prev.NextNode = newNode;
newNode.NextNode = current;
nodesCount++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void DeleteAtIndex(int index)
{
Node current = GetNode(index);
if (current == null)
{
return;
}
Node prev = GetNode(index - 1);
if (prev == null)
{
Head = Head.NextNode;
return;
}
Node next = current.NextNode;
prev.NextNode = next;
nodesCount--;
}
}
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x)
{
val = x;
next = null;
}
}
public static class LinkedListSolution
{
public static bool HasCycle(ListNode head)
{
if (head == null)
{
return false;
}
ListNode firstPointer = head;
ListNode secondPointer = head.next;
while (firstPointer != secondPointer)
{
if (secondPointer == null || secondPointer.next == null)
{
return false;
}
firstPointer = firstPointer.next;
secondPointer = secondPointer.next.next;
}
return true;
}
public static ListNode DetectCycle(ListNode head)
{
if (head == null)
{
return null;
}
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null)
{
slow = slow.next;
fast = fast.next.next;
if (fast == slow)
{
break;
}
}
if (fast == null || fast.next == null) return null;
slow = head;
while (slow != fast)
{
slow = slow.next;
fast = fast.next;
}
return fast;
}
}
}