Skip to content

Instantly share code, notes, and snippets.

@vin18
Created July 10, 2020 11:57
Show Gist options
  • Save vin18/560f17c97198844beba24b820ce1462e to your computer and use it in GitHub Desktop.
Save vin18/560f17c97198844beba24b820ce1462e to your computer and use it in GitHub Desktop.
// Function to delete node from a linked list
public static Node<Integer> delete(Node<Integer> data, int pos) {
if (head == null) {
return head;
}
int i = 0;
if (pos == 0) {
return head.next;
}
Node<Integer> temp = head;
while (temp != null && i < pos - 1) {
temp = temp.next;
i++;
}
if (temp != null && temp.next != null) {
temp.next = temp.next.next;
}
return head;
}
// Time Complexity: O(N)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment