We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static SinglyLinkedListNode deleteNode(SinglyLinkedListNode llist, int position) {
if (llist == null) return null; // If the list is empty, return null
if (position == 0) return llist.next; // If deleting the head, return the next node
SinglyLinkedListNode prev = llist;
for (int i = 0; prev.next != null && i < position - 1; i++) {
prev = prev.next;
}
if (prev.next != null) {
prev.next = prev.next.next; // Skip the target node
}
return llist;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Delete a Node
You are viewing a single comment's thread. Return to all comments →
public static SinglyLinkedListNode deleteNode(SinglyLinkedListNode llist, int position) { if (llist == null) return null; // If the list is empty, return null