-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglylist.ts
45 lines (38 loc) · 900 Bytes
/
singlylist.ts
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
class Node {
value: any;
next: Node | null;
constructor(value: any) {
this.value = value;
this.next = null;
}
}
class SinglyLinkedList {
head: Node | null;
constructor() {
this.head = null;
}
append(value: any): void {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
printList(): void {
let current = this.head;
while (current) {
console.log(current.value);
current = current.next;
}
}
}
const singlyLinkedList = new SinglyLinkedList();
singlyLinkedList.append(1);
singlyLinkedList.append(2);
singlyLinkedList.append(3);
singlyLinkedList.printList();