AlgoDaily Solution

1var assert = require('assert');
2
3class MyLinkedList {
4  constructor() {
5    this.head = null;
6    this.tail = null;
7  }
8
9  prepend(newVal) {
10    const currentHead = this.head;
11    const newNode = new LinkedListNode(newVal);
12    newNode.next = currentHead;
13    this.head = newNode;
14
15    if (!this.tail) {
16      this.tail = newNode;
17    }
18  }
19
20  append(newVal) {
21    const newNode = new LinkedListNode(newVal);
22    if (!this.head) {
23      this.head = newNode;
24      this.tail = newNode;
25    } else {
26      this.tail.next = newNode;
27      this.tail = newNode;
28    }
29  }
30
31  toString() {
32    const toPrint = [];
33    let currNode = this.head;
34
35    while (currNode) {
36      toPrint.push(currNode.val);
37      currNode = currNode.next;
38    }
39
40    return toPrint.join(' -> ');
41  }
42}
43
44function Node(val) {
45  this.val = val;
46  this.next = null;
47}
48
49function LinkedListNode(val) {
50  this.val = val;
51  this.next = null;
52}
53
54var list1 = new LinkedListNode(3);
55var nodes1 = [4, 5, 6, 7, 8, 9, 10];
56createNodes(list1, nodes1);
57
58var list2 = new LinkedListNode(1);
59var nodes2 = [2, 3, 4, 5, 6, 7, 8];
60createNodes(list2, nodes2);
61
62function createNodes(head, nodes) {
63  for (let i = 0; i < nodes.length; i++) {
64    var newNode = new LinkedListNode(nodes[i]);
65    head.next = newNode;
66    head = newNode;
67  }
68}
69
70try {
71  var linkedList1 = new MyLinkedList();
72  linkedList1.prepend(25);
73  linkedList1.prepend(15);
74  linkedList1.prepend(5);
75  linkedList1.prepend(9);
76
77  assert.deepEqual(listToString(linkedList1.head), '9 -> 5 -> 15 -> 25');
78
79  console.log(
80    'PASSED: Calling #prepend on 25, 15, 5, and 9 should equal `9 -> 5 -> 15 -> 25`'
81  );
82} catch (err) {
83  console.log(err);
84}
85
86try {
87  var linkedList3 = new MyLinkedList();
88  linkedList3.prepend(25);
89  linkedList3.prepend(15);
90  linkedList3.prepend(5);
91  linkedList3.prepend(9);
92
93  linkedList3.append(15);
94  linkedList3.append(5);
95  linkedList3.append(9);
96
97  assert.deepEqual(
98    listToString(linkedList3.head),
99    '9 -> 5 -> 15 -> 25 -> 15 -> 5 -> 9'
100  );
101
102  console.log(
103    'PASSED: Calling #append on 15, 5, and 9 should equal `9 -> 5 -> 15 -> 25 -> 15 -> 5 -> 9`'
104  );
105} catch (err) {
106  console.log(err);
107}
108
109try {
110  var linkedList2 = new MyLinkedList();
111  linkedList2.prepend(15);
112  linkedList2.append(5);
113  linkedList2.prepend(7);
114  linkedList2.append(19);
115
116  assert.deepEqual(
117    listToString(linkedList2.head),
118    '7 -> 15 -> 5 -> 19',
119    "Prepending 15, appending 5, prepending 7, appending 19 should equal '7 -> 15 -> 5 -> 19'"
120  );
121
122  console.log(
123    'PASSED: Prepending 15, appending 5, prepending 7, appending 19 should equal `7 -> 15 -> 5 -> 19`'
124  );
125} catch (err) {
126  console.log(err);
127}
128
129function listToString(head) {
130  var toPrint = [];
131  var currNode = head;
132  while (currNode) {
133    toPrint.push(currNode.val);
134    currNode = currNode.next;
135  }
136  return toPrint.join(' -> ');
137}

Community Solutions

Community solutions are only available for premium users.

Access all course materials today

The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

Returning members can login to stop seeing this.

JAVASCRIPT
OUTPUT
Results will appear here.