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 e9a2a8a

Browse files
author
Swastikyadav
committed
Add insertLast method in linkedList class
1 parent eacbd9f commit e9a2a8a

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

‎18-linkedList.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ class LinkedList {
9191

9292
previousNode.next = null;
9393
}
94+
95+
// insertLast - Insert a node at the last, create a new tail.
96+
insertLast(data) {
97+
const lastNode = new Node(data);
98+
const currentLastNode = this.getLast();
99+
100+
if (lastNode) {
101+
// There are existing nodes in chain.
102+
currentLastNode.next = lastNode;
103+
} else {
104+
// The chain is empty.
105+
this.head = new Node(data);
106+
}
107+
}
94108
}
95109

96110
// -------------------------
@@ -108,6 +122,8 @@ console.log(list);
108122

109123
// list.clear();
110124
// list.removeFirst();
111-
list.removeLast();
125+
// list.removeLast();
126+
127+
list.insertLast(30);
112128

113129
console.log(list);

0 commit comments

Comments
(0)

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