Skip to content

Commit 22608c3

Browse files
committed
adding fromLast problem
1 parent 60432d5 commit 22608c3

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed

fromLast/directions.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# --- Directions
2+
3+
Given a linked list and integer
4+
spaces from the last node in the
5+
'size' method of the linked list
6+
be less than the length of the l
7+
8+
# --- Examples
9+
10+
const list = new List();
11+
list.insertLast('a');
12+
list.insertLast('b');
13+
list.insertLast('c');
14+
list.insertLast('d');
15+
fromLast(list, 2).data // 'b'
16+
17+
18+
# --- Solution
19+
20+
function fromLast(list, n) {
21+
let slow = list.getFirst();
22+
let fast = list.getFirst();
23+
24+
while( n > 0) {
25+
fast = fast.next;
26+
n--;
27+
}
28+
29+
while(fast.next) {
30+
slow = slow.next;
31+
fast = fast.next;
32+
}
33+
34+
return slow;
35+
}

fromLast/fromLast.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// --- Directions
2+
// Given a linked list and integer
3+
// spaces from the last node in the
4+
// 'size' method of the linked list
5+
// be less than the length of the l
6+
// --- Examples
7+
// const list = new List();
8+
// list.insertLast('a');
9+
// list.insertLast('b');
10+
// list.insertLast('c');
11+
// list.insertLast('d');
12+
// fromLast(list, 2).data // 'b'
13+
14+
// Solution
15+
16+
function fromLast(list, n) {
17+
let slow = list.getFirst();
18+
let fast = list.getFirst();
19+
20+
while( n > 0) {
21+
fast = fast.next;
22+
n--;
23+
}
24+
25+
while(fast.next) {
26+
slow = slow.next;
27+
fast = fast.next;
28+
}
29+
30+
return slow;
31+
}

fromLast/linkedlist.js

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// --- Directions
2+
// Implement classes Node and Linked Lists
3+
// See 'directions' document
4+
5+
class Node {
6+
constructor(data, next = null) {
7+
this.data = data;
8+
this.next = next;
9+
}
10+
}
11+
12+
class LinkedList {
13+
constructor() {
14+
this.head = null;
15+
}
16+
17+
insertFirst(data) {
18+
// this.head = new Node(data, this.head);
19+
this.insertAt(data,0);
20+
}
21+
22+
size() {
23+
let counter = 0;
24+
let node = this.head;
25+
while(node) {
26+
counter++;
27+
node = node.next;
28+
}
29+
return counter;
30+
}
31+
32+
getFirst() {
33+
// return this.head;
34+
return this.getAt(0);
35+
}
36+
37+
getLast() {
38+
// if(!this.head) {
39+
// return null;
40+
// }
41+
42+
// let node = this.head;
43+
// while(node) {
44+
// if(!node.next) {
45+
// return node;
46+
// }
47+
// node = node.next;
48+
// }
49+
50+
return this.getAt(this.size()-1);
51+
}
52+
53+
clear() {
54+
this.head = null;
55+
}
56+
57+
removeFirst() {
58+
// if(!this.head) {
59+
// return;
60+
// }
61+
62+
// this.head = this.head.next;
63+
this.removeAt(0);
64+
}
65+
66+
removeLast() {
67+
68+
// if(!this.head) {
69+
// return;
70+
// } else if(!this.head.next) {
71+
// previous = null;
72+
// return;
73+
// }
74+
75+
// let previous = this.head;
76+
// let node = this.head.next;
77+
78+
// while(node.next) {
79+
// previous = node;
80+
// node = node.next;
81+
// }
82+
83+
// previous.next = null;
84+
85+
this.removeAt(this.size()-1);
86+
}
87+
88+
insertLast(data) {
89+
// if(!this.head) {
90+
// this.head = new Node(data);
91+
// } else {
92+
// this.getLast().next = new Node(data);
93+
// }
94+
this.insertAt(data, this.size()-1);
95+
}
96+
97+
getAt(index) {
98+
let counter = 0;
99+
let node = this.head;
100+
while(node) {
101+
if(counter === index) {
102+
return node;
103+
}
104+
105+
counter++;
106+
node = node.next;
107+
}
108+
109+
return null;
110+
}
111+
112+
removeAt(index) {
113+
if(!this.head) {
114+
return;
115+
}
116+
117+
if(index === 0) {
118+
this.head = this.head.next;
119+
return;
120+
}
121+
122+
const previous = this.getAt(index-1);
123+
if(!previous || !previous.next) {
124+
return;
125+
}
126+
previous.next = previous.next.next;
127+
}
128+
129+
insertAt(data, index) {
130+
if(!this.head) {
131+
this.head = new Node(data);
132+
return;
133+
}
134+
135+
if(index === 0) {
136+
this.head = new Node(data, this.head);
137+
return;
138+
}
139+
140+
const previous = this.getAt(index-1) || this.getLast();
141+
const node = new Node(data, previous.next);
142+
previous.next = node;
143+
}
144+
145+
forEach(fn) {
146+
let node = this.head;
147+
let counter = 0;
148+
while (node) {
149+
fn(node, counter);
150+
node = node.next;
151+
counter++;
152+
}
153+
}
154+
155+
*[Symbol.iterator]() {
156+
let node = this.head;
157+
while (node) {
158+
yield node;
159+
node = node.next;
160+
}
161+
}
162+
}
163+
164+
module.exports = { Node, LinkedList };

0 commit comments

Comments
 (0)