Reputation: 87
I have written code to delete a node from linked list given a key. However, when I try to delete the first node here and then traverse my list, it is still showing the first node that previously existed. Can someone tell me what I am doing wrong here? My entire code starting with class name
public class LinkedList {
//removing Node nested class
public void buildList1() {
head=new Node(1);
head.next=new Node(3);
head.next.next=new Node(5);
head.next.next.next=new Node(7);
}
public boolean removeNode(Node head,int x) {
//1 3 5 7---to delete 5
Node q=head;//q
// Node p=head.next;//p
Node prev=null;
if(q!=null && q.data==x) {
head=q.next;
//q=null;
System.out.println("next to head" + head.data);
return true;
}
while(q!=null && q.data!=x) {
prev=q;
q=q.next;
}
if(q==null)
return false;
prev.next=q.next;
return true;
}
public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
}
public static void main(String args[]) {
LinkedList list=new LinkedList();
list.buildList1();
list.printList();
System.out.println(list.removeNode(list.head, 1));
list.printList();
}
}
Upvotes: 2
Views: 364
Reputation: 617
@JD D had a good answer, but I would do the removeNode
method even easier.
public boolean removeNode(int x) {
tempNode = this.head;
prevNode = null;
if (this.head != null && this.head.data == x) {
this.head = this.head.next;
return true;
}
while (tempNode != null) {
if (tempNode.data == x) {
prevNode.next = tempNode.next;
return true;
}
prevNode = tempNode;
tempNode = tempNode.next;
}
return false;
}
Upvotes: 1
Reputation: 469
Try to use the list directly which you have created. Possible two solutions. First: Use the list created
if (q != null && q.data == x) {
this.head = q.next;//use this to refer the list you created
// q=null;
System.out.println("next to head" + head.data);
return true;
}
Second: Pass the list itself
public boolean removeNode(LinkedList list, int x) {
// 1 3 5 7---to delete 5
Node q = list.head;// q
}
//Call it like this
System.out.println(list.removeNode(list, 1));
Upvotes: 0
Reputation: 8097
Add head as instance variable and remove that parameter from your removeNode function. You should be able to reference this variable in your methods using the this
keyword.
Something like this (untested, but hope you get the idea):
public class LinkedList {
//removing Node nested class
private Node head;
public void buildList1() {
this.head=new Node(1);
this.head.next=new Node(3);
this.head.next.next=new Node(5);
this.head.next.next.next=new Node(7);
}
public boolean removeNode(int x) {
Node q=this.head;
Node prev=null;
if(q!=null && q.data==x) {
this.head=q.next;
return true;
}
while(q!=null && q.data!=x) {
prev=q;
q=q.next;
}
if(q==null)
return false;
prev.next=q.next;
return true;
}
public void printList()
{
Node tnode = this.head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
}
public static void main(String args[]) {
LinkedList list=new LinkedList();
list.buildList1();
list.printList();
System.out.println(list.removeNode(1));
list.printList();
}
}
Upvotes: 1
Reputation: 425003
Your life will be simpler if your head
node is not be your first node, and your first real node is be next
of your (fixed) head
.
That way, you won’t need special code to handle the case when the first (real) node is deleted.
Upvotes: 0
Reputation: 41
public static void main(String args[]) {
LinkedList list=new LinkedList();
list.buildList1();
list.printList();
list.remove(2);
list.printList();
}
LinkedList has a library of methods, one being remove(int index). Use it as shown.
Upvotes: 0