Created
July 10, 2020 11:57
-
-
Save vin18/560f17c97198844beba24b820ce1462e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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