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 ea3deaa

Browse files
author
Swastikyadav
committed
Add getFirst and getLast methods to linkedList class
1 parent 467be91 commit ea3deaa

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

‎18-linkedList.js‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ class LinkedList {
3939

4040
return listSize;
4141
}
42+
43+
// GetFirst - Returns the first node (the head) in the linked list.
44+
getFirst() {
45+
return this.head;
46+
}
47+
48+
// GetLast - Return the last node (the tail) in the linked list.
49+
getLast() {
50+
let listNode = this.head;
51+
52+
if (!this.head) return null;
53+
54+
while (listNode) {
55+
if (!listNode.next) return listNode;
56+
57+
listNode = listNode.next;
58+
}
59+
60+
return listNode;
61+
}
4262
}
4363

4464
// -------------------------
@@ -50,4 +70,6 @@ list.head = nodeOne;
5070
list.insertFirst(15);
5171

5272
console.log(list);
53-
console.log(list.size());
73+
console.log(list.size());
74+
console.log(list.getFirst());
75+
console.log(list.getLast());

0 commit comments

Comments
(0)

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