Skip to content

Commit 5f1c694

Browse files
authored
Update SinglyLinkedList.java
Added a poll() function that: - returns the head(first node) isolated and removes it from Linked-List - Updates the size of Linked-List
1 parent 208e1e9 commit 5f1c694

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,23 @@ public void deleteHead() {
347347
public void delete() {
348348
deleteNth(size - 1);
349349
}
350-
350+
351+
/**
352+
* Returns the head(first node) isolated and removes it from the
353+
* Linked-List
354+
* Updates the size of Linked-List
355+
*/
356+
public Node poll() {
357+
if (this.head == null)
358+
throw new NoSuchElementException();
359+
else {
360+
Node destruct = head;
361+
head = head.next;
362+
destruct.next = null;
363+
size--;
364+
return destruct;
365+
}
366+
}
351367
/**
352368
* Deletes an element at Nth position
353369
*/

0 commit comments

Comments
 (0)