I have Delete the node in circularly linked list in java . Deleting a Node in the beginning , middle and ending a node . How to sort out this problem
public class Node {
int element;
Node next;
}
Now there is another class All Methods of add and display are working correctly in a circular list but my remove method is having problem
public class MyLinkedList {
Node head;
void remove(int e){
Node tNode = head;
Node pNode = head;
if (head.element == e)
while(tNode.next!=head)
tNode.next=head.next.next;
else {
while (tNode != head && tNode.element != e) {
pNode=tNode.next;
}
if (tNode != head)
pNode.next = tNode.next;
}
}
}
What about
private Node remove(int i) {
Node node = head;
do {
if (node.next.element == i) {
Node n = node.next;
node.next = n.next;
if (n == head) { // removal of head
head = node;
}
return n;
}
node = node.next();
} while(node != head);
return null;
}
It removes correctly head of middle, it will simply refuse to remove last element (when list contains only one element)
NOTA : as you asked for circular linked list I assume last element has head
for its next.