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 7f0d69b

Browse files
adding insertAt method
1 parent 36652e8 commit 7f0d69b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

‎linkedList/linkedList.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,21 @@ class LinkedList {
117117
}
118118
previous.next = previous.next.next;
119119
}
120+
121+
insertAt(data, index) {
122+
if(!this.head) {
123+
this.head = new Node(data);
124+
return;
125+
}
126+
127+
if(index === 0) {
128+
this.head = new Node(data, this.head);
129+
return;
130+
}
131+
132+
const previous = this.getAt(index-1) || this.getLast();
133+
const node = new Node(data, previous.next);
134+
previous.next = node;
135+
}
120136
}
121137

0 commit comments

Comments
(0)

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