Skip to content

Commit

Permalink
Delete from LinkedList without head
Browse files Browse the repository at this point in the history
  • Loading branch information
rusty-sj committed Sep 13, 2020
1 parent 67c1489 commit c4886a4
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions General/DeleteWithoutHeadLL.java
@@ -1,4 +1,28 @@
package PACKAGE_NAME;

/**
* Due to not having head pointer, there is no way of going a node previous to the node to be deleted
* Thus, we copy the value of node next to the node to be deleted and then delete the next node
*
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
public class DeleteWithoutHeadLL {
void deleteNode(Node node) {
// Your code here
if (node.next == null) { // If node to be deleted is the last node
node = null;
return;
}
node.data = node.next.data; // Copy next element's value
node.next = node.next.next; // Delete next element
}
}

class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}

0 comments on commit c4886a4

Please sign in to comment.