Singly Linked Lists
This is being implemented in a Typescript
class Nodes {
val: string;
next: null | Nodes;
constructor(val) {
this.val = val;
this.next = null;
}
}
class SinglyList {
head: Nodes | null;
tail: Nodes | null;
length: number;
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
}
const item = new SinglyList();Push
Using
pushyou would be able to assign a continuous list.We add
!this.headbecause if there isn't a head initially, then we have to assign the current newNode to the headthis.tail.nextassures that we include the CURRENTthis.tailwhich is aNodesstructure. But itsnextvalue is null. Which is good because that's what it defaults to. But when we add a new value, we have to "push" our NEW node tonextand then REASSIGNthis.nextto the NEW value
Pop
Last updated