-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution2.cs
More file actions
169 lines (140 loc) · 4.24 KB
/
Solution2.cs
File metadata and controls
169 lines (140 loc) · 4.24 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cSharpSolutions
{
internal class Solution2
{
// Node class
private class Node
{
public int Value;
public Node Next;
public Node(int value)
{
Value = value;
Next = null;
}
}
// LinkedList class
private class SinglyLinkedList
{
private Node head;
// Add at end
public void Append(int value)
{
var newNode = new Node(value);
if (head == null)
{
head = newNode;
return;
}
Node current = head;
while (current.Next != null)
current = current.Next;
current.Next = newNode;
}
// Add at beginning
public void Prepend(int value)
{
var newNode = new Node(value)
{
Next = head
};
head = newNode;
}
// Delete first occurrence
public bool Delete(int value)
{
if (head == null) return false;
if (head.Value == value)
{
head = head.Next;
return true;
}
Node current = head;
while (current.Next != null && current.Next.Value != value)
current = current.Next;
if (current.Next == null) return false;
current.Next = current.Next.Next;
return true;
}
// Search for value
public bool Contains(int value)
{
Node current = head;
while (current != null)
{
if (current.Value == value)
return true;
current = current.Next;
}
return false;
}
// Reverse in-place
public void Reverse()
{
Node prev = null;
Node current = head;
Node next = null;
while (current != null)
{
next = current.Next;
current.Next = prev;
prev = current;
current = next;
}
head = prev;
}
// Print list
public void Print()
{
Node current = head;
Console.Write("List: ");
while (current != null)
{
Console.Write(current.Value + " -> ");
current = current.Next;
}
Console.WriteLine("null");
}
}
// Run method
public static void Run()
{
Console.WriteLine("=== Solution 2: Singly Linked List ===");
var list = new SinglyLinkedList();
Console.WriteLine("Appending 1, 2, 3, 4, 5, 6, 7, 8, 9");
list.Append(1);
list.Append(2);
list.Append(3);
list.Append(4);
list.Append(5);
list.Append(6);
list.Append(7);
list.Append(8);
list.Append(9);
list.Print();
Console.WriteLine("Prepending 0");
list.Prepend(0);
list.Print();
Console.WriteLine("Deleting 2:");
list.Delete(2);
list.Print();
Console.WriteLine("Deleting 5:");
list.Delete(5);
list.Print();
Console.WriteLine("Deleting 9:");
list.Delete(9);
list.Print();
Console.WriteLine("Reversing list:");
list.Reverse();
list.Print();
Console.WriteLine($"Contains 0? {list.Contains(0)}");
Console.WriteLine($"Contains 2? {list.Contains(2)}");
Console.WriteLine("\nSolution 2 complete");
}
}
}