|
1 | | -import { creatLinkedListFromArray } from './1-implement-linked-list.mjs'; |
2 | | -import { display } from './1-implement-linked-list.mjs'; |
| 1 | +class Node { |
| 2 | + constructor(data) { |
| 3 | + this.data = data; |
| 4 | + this.next = null; |
| 5 | + } |
| 6 | +} |
3 | 7 |
|
4 | | -/** |
5 | | - * 2->3->4->5->6->null |
6 | | - * R L |
7 | | - * |
8 | | - * 2->null |
9 | | - * |
10 | | - * 3->2->null |
11 | | - * R |
12 | | - * 4->5->6->null |
13 | | - * L |
14 | | - * |
15 | | - * 4->3->2->null |
16 | | - * R |
17 | | - * 5->6->null |
18 | | - * L |
19 | | - * |
20 | | - * 5->4->3->2->null |
21 | | - * R |
22 | | - * 6->null |
23 | | - * L |
24 | | - * |
25 | | - * 6->5->4->3->2->null |
26 | | - * R |
27 | | - * |
28 | | - */ |
| 8 | +function insert(head, val) { |
| 9 | + const node = new Node(val); |
| 10 | + if (head == null) { |
| 11 | + return node; |
| 12 | + } else { |
| 13 | + node.next = head; |
| 14 | + } |
29 | 15 |
|
30 | | -/** |
31 | | - * 2->3->4->5->6 |
32 | | - * |
33 | | - * R->2; |
34 | | - * L ->3; |
35 | | - * |
36 | | - * R.next = null |
37 | | - * l points next |
38 | | - * temp = L.next |
39 | | - L.next = R |
40 | | - R = L |
41 | | - L = temp |
| 16 | + return node; |
| 17 | +} |
42 | 18 |
|
43 | | - * L.next = R |
44 | | - R = L |
45 | | - */ |
| 19 | +function main() { |
| 20 | + let head = null; |
| 21 | + head = insert(head, 2); |
| 22 | + head = insert(head, 4); |
| 23 | + head = insert(head, 6); |
| 24 | + head = insert(head, 8); |
46 | 25 |
|
47 | | -/** |
48 | | - * runtime complexity: O(n) because reverse the list in single loop |
49 | | - * space/memory complexity: O(1) because it is iterative |
50 | | - * @param head head |
51 | | - * @returns reversed head |
52 | | - */ |
53 | | -function reverse(head) { |
54 | | - let reverse = head; |
55 | | - let listToDo = head.next; |
| 26 | + console.log(print(head)); |
| 27 | + |
| 28 | + return head; |
| 29 | +} |
56 | 30 |
|
57 | | - head.next = null; |
| 31 | +function print(head) { |
| 32 | + if (!head) return ''; |
58 | 33 |
|
59 | | - while (listToDo) { |
60 | | - let temp = listToDo.next; |
61 | | - listToDo.next = reverse; |
62 | | - reverse = listToDo; |
63 | | - listToDo = temp; |
| 34 | + let current = head; |
| 35 | + let s = ''; |
| 36 | + |
| 37 | + while (current.next) { |
| 38 | + s += ' ' + current.data; |
| 39 | + current = current.next; |
64 | 40 | } |
65 | 41 |
|
66 | | - return reverse; |
| 42 | + s += ' ' + current.data; |
| 43 | + |
| 44 | + return s.trim(); |
67 | 45 | } |
68 | 46 |
|
69 | | -const head = creatLinkedListFromArray([2, 3, 4, 5, 6]); |
70 | | -const reversedHead = reverse(head); |
71 | | -console.log(display(reversedHead)); |
| 47 | +// O(n) time | O(1) space |
| 48 | +function reverse(head) { |
| 49 | + let current = head; |
| 50 | + let previous = null; |
| 51 | + let next = null; |
| 52 | + |
| 53 | + while (current) { |
| 54 | + next = current.next; |
| 55 | + current.next = previous; |
| 56 | + previous = current; |
| 57 | + current = next; |
| 58 | + } |
| 59 | + |
| 60 | + head = previous; |
72 | 61 |
|
73 | | -/** TEST |
74 | | - * 2-3-4-5-6 |
75 | | - * R L |
76 | | - * |
77 | | - * 2-null |
78 | | - * |
79 | | - * 3-2-null |
80 | | - * R |
81 | | - * 4-5-6-null |
82 | | - * L |
83 | | - * |
84 | | - * 4-3-2-null |
85 | | - * R |
86 | | - * 5-6-null |
87 | | - * L |
88 | | - * |
89 | | - * 5-4-3-2-null |
90 | | - * R |
91 | | - * 6-null |
92 | | - * L |
93 | | - * |
94 | | - * 6-5-4-3-2-null |
95 | | - * R |
96 | | - * |
97 | | - * null |
98 | | - * L |
99 | | - */ |
| 62 | + return head; |
| 63 | +} |
0 commit comments