Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | 19x 10242x 10242x 13880x 99991043x 20269x 19x 84x 84x 84x 40436x 50x 50025169x 20193x 19x 10183x 10183x 35x 35x 10148x 10148x 10183x 10007x 10005x 2x 2x 2x 2x 2x 10003x 10003x 49985002x 10003x 10003x 10003x 10003x 10003x 7x 5x 5x 5x 5x 6x 6x 3x 3x 3x 3x 6x 2x 1x 1x 1x 1x 2x 2x 2x 4x 3x 2x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 7x 2x 2x 2x 2x 2x 5x 5x 1x 3x 3x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 23x 23x 23x 48x 48x 23x 4x 2x 2x 2x 2x 6x 6x 6x 6x 2x 2x 2x 4x 1x 3x 1x 2x 2x 2x 5x 1x 4x 4x 1x 4x 4x 58x 3x 55x 1x 53x 53x 53x 53x 1x 1x 52x 52x 3630x 51x 51x 51x 51x 51x 3579x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 12x 4x 12x 4x | /**
* data-structure-typed
*
* @author Tyler Zeng
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
* @license MIT License
*/
export class SinglyLinkedListNode<T = number> {
/**
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
* @param {T} val - The "val" parameter is of type T, which means it can be any data type. It represents the value that
* will be stored in the node of a linked list.
*/
constructor(val: T) {
this._val = val;
this._next = null;
}
private _val: T;
get val(): T {
return this._val;
}
set val(value: T) {
this._val = value;
}
private _next: SinglyLinkedListNode<T> | null;
get next(): SinglyLinkedListNode<T> | null {
return this._next;
}
set next(value: SinglyLinkedListNode<T> | null) {
this._next = value;
}
}
export class SinglyLinkedList<T = any> {
/**
* The constructor initializes the linked list with an empty head, tail, and length.
*/
constructor() {
this._head = null;
this._tail = null;
this._length = 0;
}
private _head: SinglyLinkedListNode<T> | null;
get head(): SinglyLinkedListNode<T> | null {
return this._head;
}
set head(value: SinglyLinkedListNode<T> | null) {
this._head = value;
}
private _tail: SinglyLinkedListNode<T> | null;
get tail(): SinglyLinkedListNode<T> | null {
return this._tail;
}
set tail(value: SinglyLinkedListNode<T> | null) {
this._tail = value;
}
private _length: number;
get length(): number {
return this._length;
}
/**
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
* array.
* @param {T[]} data - The `data` parameter is an array of elements of type `T`.
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
*/
static fromArray<T>(data: T[]) {
const singlyLinkedList = new SinglyLinkedList<T>();
for (const item of data) {
singlyLinkedList.push(item);
}
return singlyLinkedList;
}
getLength(): number {
return this._length;
}
/**
* The `push` function adds a new node with the given data to the end of a singly linked list.
* @param {T} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
* any type (T) as specified in the generic type declaration of the class or function.
*/
push(data: T): void {
const newNode = new SinglyLinkedListNode(data);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail!.next = newNode;
this.tail = newNode;
}
this._length++;
}
/**
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
* pointers accordingly.
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
* the linked list is empty, it returns `null`.
*/
pop(): T | undefined {
if (!this.head) return undefined;
if (this.head === this.tail) {
const val = this.head.val;
this.head = null;
this.tail = null;
this._length--;
return val;
}
let current = this.head;
while (current.next !== this.tail) {
current = current.next!;
}
const val = this.tail!.val;
current.next = null;
this.tail = current;
this._length--;
return val;
}
/**
* The `shift()` function removes and returns the value of the first node in a linked list.
* @returns The value of the node that is being removed from the beginning of the linked list.
*/
shift(): T | undefined {
if (!this.head) return undefined;
const removedNode = this.head;
this.head = this.head.next;
this._length--;
return removedNode.val;
}
/**
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
* @param {T} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
* linked list.
*/
unshift(val: T): void {
const newNode = new SinglyLinkedListNode(val);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head = newNode;
}
this._length++;
}
/**
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
* @param {number} index - The index parameter is a number that represents the position of the element we want to
* retrieve from the list.
* @returns The method `getAt(index: number): T | null` returns the value at the specified index in the linked list, or
* `null` if the index is out of bounds.
*/
getAt(index: number): T | undefined {
if (index < 0 || index >= this.length) return undefined;
let current = this.head;
for (let i = 0; i < index; i++) {
current = current!.next;
}
return current!.val;
}
/**
* The function `getNodeAt` returns the node at a given index in a singly linked list.
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<T>` object if the node at the
* specified index exists, or `null` if the index is out of bounds.
*/
getNodeAt(index: number): SinglyLinkedListNode<T> | null {
let current = this.head;
for (let i = 0; i < index; i++) {
current = current!.next;
}
return current;
}
/**
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
* data structure. It is of type number.
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
* bounds.
*/
deleteAt(index: number): T | undefined {
if (index < 0 || index >= this.length) return undefined;
if (index === 0) return this.shift();
if (index === this.length - 1) return this.pop();
const prevNode = this.getNodeAt(index - 1);
const removedNode = prevNode!.next;
prevNode!.next = removedNode!.next;
this._length--;
return removedNode!.val;
}
delete(valueOrNode: T): boolean;
delete(valueOrNode: SinglyLinkedListNode<T>): boolean;
/**
* The delete function removes a node with a specific value from a singly linked list.
* @param {T | SinglyLinkedListNode<T>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `T`
* or a `SinglyLinkedListNode<T>` object.
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
*/
delete(valueOrNode: T | SinglyLinkedListNode<T>): boolean {
let value: T;
Iif (valueOrNode instanceof SinglyLinkedListNode) {
value = valueOrNode.val;
} else {
value = valueOrNode;
}
let current = this.head,
prev = null;
while (current) {
if (current.val === value) {
Iif (prev === null) {
this.head = current.next;
Iif (current === this.tail) {
this.tail = null;
}
} else {
prev.next = current.next;
Iif (current === this.tail) {
this.tail = prev;
}
}
this._length--;
return true;
}
prev = current;
current = current.next;
}
return false;
}
/**
* The `insertAt` function inserts a value at a specified index in a singly linked list.
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
* linked list. It is of type number.
* @param {T} val - The `val` parameter represents the value that you want to insert into the linked list at the
* specified index.
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
* if the index is out of bounds.
*/
insertAt(index: number, val: T): boolean {
Iif (index < 0 || index > this.length) return false;
if (index === 0) {
this.unshift(val);
return true;
}
if (index === this.length) {
this.push(val);
return true;
}
const newNode = new SinglyLinkedListNode(val);
const prevNode = this.getNodeAt(index - 1);
newNode.next = prevNode!.next;
prevNode!.next = newNode;
this._length++;
return true;
}
/**
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
* whether it is empty or not.
* @returns A boolean value indicating whether the length of the object is equal to 0.
*/
isEmpty(): boolean {
return this.length === 0;
}
/**
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
*/
clear(): void {
this._head = null;
this._tail = null;
this._length = 0;
}
/**
* The `toArray` function converts a linked list into an array.
* @returns The `toArray()` method is returning an array of type `T[]`.
*/
toArray(): T[] {
const array: T[] = [];
let current = this.head;
while (current) {
array.push(current.val);
current = current.next;
}
return array;
}
/**
* The `reverse` function reverses the order of the nodes in a singly linked list.
* @returns The reverse() method does not return anything. It has a return type of void.
*/
reverse(): void {
if (!this.head || this.head === this.tail) return;
let prev: SinglyLinkedListNode<T> | null = null;
let current: SinglyLinkedListNode<T> | null = this.head;
let next: SinglyLinkedListNode<T> | null = null;
while (current) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
[this.head, this.tail] = [this.tail!, this.head!];
}
/**
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
* the callback function. If no element satisfies the condition, it returns `null`.
*/
find(callback: (val: T) => boolean): T | null {
let current = this.head;
while (current) {
if (callback(current.val)) {
return current.val;
}
current = current.next;
}
return null;
}
/**
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
* @param {T} value - The value parameter is the value that you want to find the index of in the linked list.
* @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
* value is not found, it returns -1.
*/
indexOf(value: T): number {
let index = 0;
let current = this.head;
while (current) {
if (current.val === value) {
return index;
}
index++;
current = current.next;
}
return -1;
}
/**
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
* null.
* @param {T} value - The value parameter is the value that we want to search for in the linked list.
* @returns a `SinglyLinkedListNode<T>` if a node with the specified value is found in the linked list. If no node with
* the specified value is found, the function returns `null`.
*/
findNode(value: T): SinglyLinkedListNode<T> | null {
let current = this.head;
while (current) {
if (current.val === value) {
return current;
}
current = current.next;
}
return null;
}
insertBefore(existingValue: T, newValue: T): boolean;
insertBefore(existingValue: SinglyLinkedListNode<T>, newValue: T): boolean;
/**
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
* @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node that you want to insert the
* new value before. It can be either the value itself or a node containing the value in the linked list.
* @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
* @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
* inserted before the existing value, and `false` otherwise.
*/
insertBefore(existingValueOrNode: T | SinglyLinkedListNode<T>, newValue: T): boolean {
Iif (!this.head) return false;
let existingValue: T;
Iif (existingValueOrNode instanceof SinglyLinkedListNode) {
existingValue = existingValueOrNode.val;
} else {
existingValue = existingValueOrNode;
}
if (this.head.val === existingValue) {
this.unshift(newValue);
return true;
}
let current = this.head;
while (current.next) {
if (current.next.val === existingValue) {
const newNode = new SinglyLinkedListNode(newValue);
newNode.next = current.next;
current.next = newNode;
this._length++;
return true;
}
current = current.next;
}
return false;
}
insertAfter(existingValueOrNode: T, newValue: T): boolean;
insertAfter(existingValueOrNode: SinglyLinkedListNode<T>, newValue: T): boolean;
/**
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
* @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node in the linked list after which
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
* @param {T} newValue - The value that you want to insert into the linked list after the existing value or node.
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
* existing value or node, and false if the existing value or node was not found in the linked list.
*/
insertAfter(existingValueOrNode: T | SinglyLinkedListNode<T>, newValue: T): boolean {
let existingNode: T | SinglyLinkedListNode<T> | null;
Iif (existingValueOrNode instanceof SinglyLinkedListNode) {
existingNode = existingValueOrNode;
} else {
existingNode = this.findNode(existingValueOrNode);
}
if (existingNode) {
const newNode = new SinglyLinkedListNode(newValue);
newNode.next = existingNode.next;
existingNode.next = newNode;
Iif (existingNode === this.tail) {
this.tail = newNode;
}
this._length++;
return true;
}
return false;
}
/**
* The function counts the number of occurrences of a given value in a linked list.
* @param {T} value - The value parameter is the value that you want to count the occurrences of in the linked list.
* @returns The count of occurrences of the given value in the linked list.
*/
countOccurrences(value: T): number {
let count = 0;
let current = this.head;
while (current) {
if (current.val === value) {
count++;
}
current = current.next;
}
return count;
}
*[Symbol.iterator]() {
let current = this.head;
while (current) {
yield current.val;
current = current.next;
}
}
}
|