Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions Data Structures in JAVA/Linked List 1/Delete_Node_in_LL.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,30 @@ public LinkedListNode(T data) {
*****************************************************************/

public class Solution {
public static LinkedListNode<Integer> deleteNode(LinkedListNode<Integer> head, int i) {
// Write your code here.
public static Node<Integer> deleteNode(Node<Integer> head, int pos) {
if (head == null) {
return null; // If the list is empty, nothing to delete
}

if(head==null )
return head;
if(i==0)
return head.next;
int count=0;
LinkedListNode<Integer> temp=head;
while(temp!=null && count<i-1)
{
temp=temp.next;
count++;
if (pos == 0) {
return head.next; // If the position is 0, delete the head node
}

int i = 0;
Node<Integer> temp = head;
while (temp != null && i < pos - 1) {
temp = temp.next;
i++;
}
if(temp==null)

// If temp is null or temp.next is null, it means the position is out of bounds
if (temp == null || temp.next == null) {
return head;
if(temp.next!=null)
temp.next=temp.next.next;

}

// Update the link to skip the node at position 'pos'
temp.next = temp.next.next;

return head;
}
}
}