-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinkedList.cs
242 lines (196 loc) · 7 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
namespace interviewPractice
{
public class linkedListDBL
{
int data { get; set; }
public linkedListDBL prev { get; set; }
public linkedListDBL next { get; set; }
public linkedListDBL(int value)
{
data = value;
}
public linkedListDBL Insert(linkedListDBL head, int data)
{
linkedListDBL targetNode = new linkedListDBL(data);
linkedListDBL currNode = head;
if (head == null)
{
//if head hasnt been defined, then the target node becomes the //first node
head = targetNode;
}
else if (data < head.data)
{
//scenario 2:
targetNode.next = currNode;
head = targetNode;
}
else
{
while (currNode.next != null && (currNode.next.data < targetNode.data))
{
currNode = currNode.next;
}
targetNode.next = currNode.next;
currNode.next = targetNode;
}
return head;
}
public linkedListDBL Reverse(linkedListDBL head)
{
linkedListDBL currNode = head;
while (currNode != null)
{
linkedListDBL saveNode = currNode.prev;
currNode.prev = currNode.next;
currNode.next = saveNode;
head = currNode;
currNode = currNode.prev;
}
return head;
}
}
public class linkedList
{
int data { get; set; }
public linkedList next { get; set; }
public linkedList(int value)
{
data = value;
}
public linkedList RotateRight(linkedList head, int k)
{
if (head == null) { return head; }
var tailNode = head;
int rotations = 0;
int position = 0;
int size = 1;
//This loop is to determine how many nodes are in the lis
while (tailNode.next != null)
{
tailNode = tailNode.next;
size++;
}
//this assignment is to dertimine the number of relative rotations
rotations = k % size;
//we create a circle by connetcting the the original tail to the head
tailNode.next = head;
//from here we cycle through to our new tail
while (position < (size - rotations))
{
tailNode = tailNode.next;
position++;
}
//Once in position we reset the Head and break the loop
head = tailNode.next;
tailNode.next = null;
// Once the loop is broken return the head
return head;
}
public linkedList DeleteDuplicates(linkedList head)
{
//==============Strategy=======================//
// this starategy works for a sorted list
//1. starting at the head node, look at the next node's data value
//2. if the current nodes data value and the next node's data value are equal
// then have the next pointer go to the "next next" node
// Note: when you ddo this DO not move the pointer, there might be several duplicates
//3. only if the data values of current and next node are differnt do you move current head forward
var currNode = head;
while (currNode != null && currNode.next != null)
{
if (currNode.data == currNode.next.data)
{
currNode.next = currNode.next.next;
}
else
{
currNode = currNode.next;
}
}
return head;
}
public linkedList ReverseList(linkedList head)
{
if (head == null) return null;
var currNode = head;
var saveNode = currNode.next;
while (currNode.next != null)
{
currNode.next = currNode.next.next;
saveNode.next = head;
head = saveNode;
saveNode = currNode.next;
}
return head;
}
public bool IsPalindrome(linkedList head)
{
var slow = head;
var fast = head;
var check = head;
//null head is read as a palindrome
//one item list are palindromes
if (head == null || head.next == null) return true;
// find the middle first: Employ the fast/slow method
// where fast moves two nodes and slow moves one node
// when fast == null or fast.next == null, we have hit the last node
// and our slow node is on the middle node.
// Note: If the # of nodes is odd, slow will be sqaurely in the middle
// If the # of nodes are even it will be at the middle node closer to the end
while (fast != null && fast.next != null)
{
slow = slow.next;
fast = fast.next.next;
}
//reverse the node from middle to end
//next heck if it was an even list or odd list
if (fast != null) //this is the odd condition
{
slow = slow.next;
}
// reversing function need a few more refernce objects
var mid = slow;
var track = slow.next;
while (slow.next != null)
{
slow.next = slow.next.next;
track.next = mid;
mid = track;
track = slow.next;
}
// checking values for same data
while (mid != null)
{
if (mid.data != check.data) return false;
mid = mid.next;
check = check.next;
}
return true;
}
public linkedList RemoveElements(linkedList head, int val)
{
var curr = head;
// this is for the edge case when the first value is the value to be deleted
// this also handles the edge case for when the whol list need to be deleted
while (head != null && curr.data == val)
{
head = curr.next;
curr = head;
}
//check for null values
if (head == null) return head;
//if the next value is equal to the vlaue to be deleted then
// skip it, if not then just move the currnode down the line
while (curr.next != null)
{
if (curr.next.data == val)
{
curr.next = curr.next.next;
}
else { curr = curr.next; }
}
return head;
}
}
}