Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60432d5

Browse files
adding circular list problem
1 parent 5af5e81 commit 60432d5

File tree

3 files changed

+229
-0
lines changed

3 files changed

+229
-0
lines changed

‎circular/circular.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// --- Directions
2+
// Given a linked list, return true if the list
3+
// is circular, false if it is not.
4+
// --- Examples
5+
// const l = new List();
6+
// const a = new Node('a');
7+
// const b = new Node('b');
8+
// const c = new Node('c');
9+
// l.head = a;
10+
// a.next = b;
11+
// b.next = c;
12+
// c.next = b;
13+
// circular(l) // true
14+
15+
16+
// Solution
17+
18+
function circular(list) {
19+
let slow = list.getFirst();
20+
let fast = list.getFirst();
21+
22+
while(fast.next && fast.next.next) {
23+
slow = slow.next;
24+
fast = fast.next.next;
25+
26+
if(slow === fast) {
27+
return true;
28+
}
29+
}
30+
31+
return false;
32+
}

‎circular/directions.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# --- Directions
2+
Given a linked list, return true if the list
3+
is circular, false if it is not.
4+
5+
# --- Examples
6+
7+
const l = new List();
8+
const a = new Node('a');
9+
const b = new Node('b');
10+
const c = new Node('c');
11+
l.head = a;
12+
a.next = b;
13+
b.next = c;
14+
c.next = b;
15+
circular(l) // true
16+
17+
# --- Solution
18+
19+
function circular(list) {
20+
let slow = list.getFirst();
21+
let fast = list.getFirst();
22+
23+
while(fast.next && fast.next.next) {
24+
slow = slow.next;
25+
fast = fast.next.next;
26+
27+
if(slow === fast) {
28+
return true;
29+
}
30+
}
31+
32+
return false;
33+
}

‎circular/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)

AltStyle によって変換されたページ (->オリジナル) /