Delete a Node HackerRank
public static SinglyLinkedListNode deleteNode(SinglyLinkedListNode head, int pos) {
SinglyLinkedListNode temp=head;
SinglyLinkedListNode old;
int i=0;
if(pos>0){
while(i<pos-1){
temp=temp.next;
i++;
}
old=temp.next.next;
// temp.next.next=null;
temp.next=old;
return head;
}
else {
old=temp.next;
temp.next=null;
head=old;
return head;
}
Comments
Post a Comment