Skip to content

Commit 016f050

Browse files
committed
shoaib/issue#5229
i have added poll method for SinglyLinkedList and also fixed whenever poll is called the size should be decreased by 1.
1 parent 308bdcf commit 016f050

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java

+11
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,17 @@ public int getNth(int index) {
383383
return cur.value;
384384
}
385385

386+
// poll method is used to remove the head elemnt and return its value
387+
public int poll() {
388+
if (head == null) {
389+
return -1; // Return a default value or handle empty case
390+
}
391+
int headValue = head.value;
392+
head = head.next;
393+
size--; // As head is removed size will be decreased
394+
return headValue;
395+
}
396+
386397
/**
387398
* @param position to check position
388399
* @param low low index

0 commit comments

Comments
 (0)