Skip to content

Commit bca1e3c

Browse files
committed
Fix LinkedList.AddAtHead(), AddAtTail() methods
1 parent 0a7c1c1 commit bca1e3c

File tree

1 file changed

+59
-23
lines changed

1 file changed

+59
-23
lines changed

LeetCode/DataStructures/LinkedList.cs

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,75 @@ public class Node
99
// Fields
1010
public int? Data;
1111
public Node Next;
12-
public Node Previous;
1312

1413
// Constructors
14+
public Node()
15+
{
16+
this.Data = null;
17+
this.Next = null;
18+
}
1519
public Node(int value)
1620
{
1721
this.Data = value;
1822
this.Next = null;
19-
this.Previous = null;
2023
}
2124
}
2225

2326
/// <summary>
2427
/// 707. Design Linked List
25-
/// Design your implemenation of the linked list. You can choose to use the singly linked list or the doubly lined list. A node in a singly linked list should have two attributes: val and next. Val is the value of the current node, next is a pointer/ reference to the next node. If you want to use the double linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.
28+
/// Design your implementation of the linked list. You can choose to use the singly linked list or the doubly lined list. A node in a singly linked list should have two attributes: val and next. Val is the value of the current node, next is a pointer/ reference to the next node. If you want to use the double linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.
2629
/// </summary>
2730
public class LinkedList
2831
{
2932
private Node _head;
3033
private Node _tail;
3134

32-
public int Count = 0;
35+
public int Count;
3336

3437
// Constructors
3538
public LinkedList()
3639
{
37-
this._head = null;
40+
this._head = new Node();
3841
this._tail = null;
42+
this.Count = 0;
3943
}
40-
public LinkedList(Node head)
44+
45+
//TODO: get(index), addAtIndex(index, val), deleteAtIndex(index)
46+
47+
/// <summary>
48+
/// Append a node to the front of the linked list
49+
/// </summary>
50+
/// <param name="val"></param>
51+
public void AddAtHead(int val)
4152
{
42-
this._head = head;
43-
if (head.Next == null)
53+
// If LinkedList is empty, set both the head and tail to this node
54+
// Else set the current list to the nodeToAdd'.Next and move the nodeToAdd to the front (head) of the LinkedList
55+
Node nodeToAdd = new Node(val);
56+
if (_head.Data == null)
4457
{
45-
Count = 1;
58+
_head = nodeToAdd;
59+
_tail = nodeToAdd;
4660
}
47-
while (head.Next != null)
61+
else
4862
{
49-
this._tail = head.Next;
50-
Count++;
63+
nodeToAdd.Next = _head;
64+
_head = nodeToAdd;
5165
}
52-
}
53-
54-
//TODO: get(index), addAtHead(val), addAtTail(val), addAtIndex(index, val), deleteAtIndex(index)
55-
56-
public void AddAtHead(int val)
57-
{
58-
66+
Count++;
5967
}
6068

6169
/// <summary>
62-
/// Append a node of values to the last element of the linked list.
70+
/// Append a node the end of the linked list.
6371
/// </summary>
6472
/// <param name="val"></param>
6573
public void AddAtTail(int val)
6674
{
75+
// If LinkedList is empty, add node to the head and exit out of function
76+
if (_head.Data == null)
77+
{
78+
AddAtHead(val);
79+
return;
80+
}
6781
try
6882
{
6983
Node node = _head; // Set Node to first node in the list
@@ -83,21 +97,43 @@ public void AddAtTail(int val)
8397
public void AddAtIndex(int index, int val)
8498
{
8599

100+
if (index < 0) return;
101+
102+
// If LinkedList is empty, add node to the front
103+
if (_head.Data == null)
104+
{
105+
AddAtHead(val);
106+
return;
107+
}
108+
109+
//If index is out of range of LinkedList, add node to the tail
110+
if (index > Count)
111+
{
112+
AddAtTail(val);
113+
return;
114+
}
115+
116+
// Iterate to the index and insert
117+
86118
}
87119

88120
public void PrintLinkedList()
89121
{
122+
Node node = _head;
123+
if (node.Data == null && node.Next == null) return; //Check if LinkedList is null & empty
124+
90125
Console.WriteLine($"707. Design Linked List");
91126
Console.WriteLine($"Print Linked List");
92127
Console.Write($"HEAD => ");
93-
Node node = _head;
94-
if (node.Data == null && node.Next == null) return; //Check if LinkedList is null & empty
128+
129+
// Iterate through each node, printing its value
95130
while (node.Data != null && node.Next != null)
96131
{
97132
Console.Write($"{node.Data} => ");
98133
node = node.Next;
99134
}
100-
135+
136+
// End of Linked List
101137
if (node.Next == null)
102138
{
103139
Console.Write($"{node.Data} => TAIL\n\n");

0 commit comments

Comments
 (0)