|
1 | | -class LinkedListNode { |
| 1 | +export class LinkedListNode { |
2 | 2 | constructor(data) { |
3 | 3 | this.data = data; |
4 | 4 | this.next = null; |
5 | 5 | } |
6 | 6 | } |
7 | 7 |
|
8 | | -class LinkedList { |
9 | | - constructor() { |
10 | | - this.head = null; |
11 | | - this.length = 0; |
12 | | - } |
13 | | - |
14 | | - insertAtHead(value) { |
15 | | - var newNode = new LinkedListNode(value); |
16 | | - newNode.next = this.head; |
17 | | - this.head = newNode; |
18 | | - this.length++; |
| 8 | +/** |
| 9 | + * O(1) |
| 10 | + * @param head |
| 11 | + * @param data |
| 12 | + * @returns |
| 13 | + */ |
| 14 | +export function insertAtHead(head, data) { |
| 15 | + const newNode = new LinkedListNode(data); |
| 16 | + newNode.next = head; |
| 17 | + |
| 18 | + return newNode; |
| 19 | +} |
19 | 20 |
|
20 | | - return newNode; |
| 21 | +/** |
| 22 | + * Time Complexity O(n) |
| 23 | + * Space Complexity O(1) |
| 24 | + * @param head head |
| 25 | + * @param node node to add at the tail |
| 26 | + * @returns head |
| 27 | + */ |
| 28 | +export function insertAtTail(head, node) { |
| 29 | + if (!head || !head.next) { |
| 30 | + return node; |
21 | 31 | } |
22 | 32 |
|
23 | | - insertAtTail(value) { |
24 | | - var newNode = new LinkedListNode(value); |
25 | | - let head = this.head; |
26 | | - |
27 | | - if (!head) { |
28 | | - head = newNode; |
29 | | - this.length++; |
| 33 | + let temp = head; |
30 | 34 |
|
31 | | - return newNode; |
32 | | - } |
33 | | - |
34 | | - let temp = head; |
35 | | - |
36 | | - while (temp.next) { |
37 | | - temp = temp.next; |
38 | | - } |
| 35 | + while (temp.next) { |
| 36 | + temp = temp.next; |
| 37 | + } |
39 | 38 |
|
40 | | - // temp is tail |
41 | | - temp.next = newNode; |
| 39 | + temp.next = node; |
42 | 40 |
|
43 | | - this.length++; |
| 41 | + return head; |
| 42 | +} |
44 | 43 |
|
45 | | - return newNode; |
| 44 | +export function createRandomLinkedList(length) { |
| 45 | + let head = null; |
| 46 | + for (let index = 0; index < length; index++) { |
| 47 | + head = insertAtHead(head, Math.floor(Math.random() * 100 + 1)); |
46 | 48 | } |
47 | 49 |
|
48 | | - toString() { |
49 | | - if (!this.head) { |
50 | | - return ''; |
51 | | - } |
| 50 | + return head; |
| 51 | +} |
52 | 52 |
|
53 | | - let temp = this.head; |
54 | | - let data = ''; |
| 53 | +export function creatLinkedListFromArray(arr) { |
| 54 | + let head = null; |
| 55 | + while (arr.length) { |
| 56 | + head = insertAtHead(head, arr.pop()); |
| 57 | + } |
55 | 58 |
|
56 | | - while (temp) { |
57 | | - data += ',' + temp.data; |
58 | | - temp = temp.next; |
59 | | - } |
| 59 | + return head; |
| 60 | +} |
60 | 61 |
|
61 | | - return data.substr(1,data.length); |
| 62 | +export function display(head) { |
| 63 | + if (!head) { |
| 64 | + return ''; |
62 | 65 | } |
63 | | -} |
64 | 66 |
|
65 | | -function createRandomLinkedList(length) { |
66 | | - const list = new LinkedList(); |
67 | | - for (let i = 0; i < length; i++) { |
68 | | - list.insertAtHead(Math.floor(Math.random() * 100 + 1)); |
| 67 | + let text = ''; |
| 68 | + let temp = head; |
| 69 | + while (temp.next) { |
| 70 | + text += `, ${temp.data}`; |
| 71 | + temp = temp.next; |
69 | 72 | } |
70 | 73 |
|
71 | | - return list; |
| 74 | + text += `, ${temp.data}`; |
| 75 | + |
| 76 | + return text.substr(1, text.length); |
72 | 77 | } |
73 | 78 |
|
74 | 79 | // Test----------------------- |
75 | | -const list = new LinkedList(); |
76 | | -list.insertAtHead(2); |
77 | | -console.log(list.toString()); |
| 80 | +/* |
| 81 | +console.log(display(insertAtHead(null, 2))); |
| 82 | +
|
| 83 | +console.log(insertAtTail(null, { data: 3 })); |
78 | 84 |
|
79 | | -list.insertAtTail(5); |
80 | | -console.log(list.toString()); |
| 85 | +console.log(display(createRandomLinkedList(20))); |
81 | 86 |
|
82 | | -console.log(createRandomLinkedList(20).toString()); |
| 87 | +console.log(display(creatLinkedListFromArray([2, 3, 4, 5, 6]))); |
| 88 | +*/ |
0 commit comments