Delete a node given a key in linked listSort a Map<Key, Value> by valuesClone a Singleton objectHow to implement Add and Delete methods using objects in a Linked ListKeep the head of a singly linked list in JavaHead node of a linked listJava delete node linked list not workingJava doubly linked list — equality of two adjacent nodesAdding to the end of a Doubly-Linked List Data Structure in JavaLinked List Recursion in Java with Node as ParameterLinked list is only displaying the head node, not sure why
Doubt about a particular point of view on how to do character creation
How to differentiate between two people with the same name in a story?
The correct way of compute indicator function in Mathematica
Four day weekend?
Why don't 3D printer heads use ceramic inner walls?
What is the following VRP?
Moscow SVO airport, how to avoid scam taxis without pre-booking?
What was Captain Marvel supposed to do once she reached her destination?
Received email from ISP saying one of my devices has malware
What's the origin of the concept of alternate dimensions/realities?
When you have to wait for a short time
Why do presidential pardons exist in a country having a clear separation of powers?
How to save money by shopping at a variety of grocery stores?
Resources to learn about firearms?
How did the Altair 8800 front panel load the program counter?
What is the chance of getting a Red Cabbage in year 1?
How is the anglicism "jackpot" commonly expressed in French?
Who declared the Last Alliance to be the "last" and why?
I was given someone else's visa, stamped in my passport
Heuristic argument for the Riemann Hypothesis
Padding a column of lists
What caused the end of cybernetic implants?
Is it good practice to speed up and slow down where not written in a song?
IList<T> implementation
Delete a node given a key in linked list
Sort a Map<Key, Value> by valuesClone a Singleton objectHow to implement Add and Delete methods using objects in a Linked ListKeep the head of a singly linked list in JavaHead node of a linked listJava delete node linked list not workingJava doubly linked list — equality of two adjacent nodesAdding to the end of a Doubly-Linked List Data Structure in JavaLinked List Recursion in Java with Node as ParameterLinked list is only displaying the head node, not sure why
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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();
java
add a comment |
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();
java
An aside:if(q.data==x && q!=null)
the conditions here should be flipped
– GBlodgett
Mar 27 at 23:16
1
Can you provide a more complete set of your code? Can you post what your entire LinkedList class looks like? YourremoveNode
method shouldn't be taking in the head node as a parameter. The head node should be an instance variable of the LinkedList class.
– JD D
Mar 27 at 23:56
YourLinkedList
class should keep track of its ownhead
, not take it as a parameter (to any of its methods) -- so it should start aspublic class LinkedList private Node head; ...
– Stephen P
Mar 28 at 0:06
add a comment |
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();
java
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();
java
java
edited Mar 28 at 0:04
user9179677
asked Mar 27 at 23:15
user9179677user9179677
525 bronze badges
525 bronze badges
An aside:if(q.data==x && q!=null)
the conditions here should be flipped
– GBlodgett
Mar 27 at 23:16
1
Can you provide a more complete set of your code? Can you post what your entire LinkedList class looks like? YourremoveNode
method shouldn't be taking in the head node as a parameter. The head node should be an instance variable of the LinkedList class.
– JD D
Mar 27 at 23:56
YourLinkedList
class should keep track of its ownhead
, not take it as a parameter (to any of its methods) -- so it should start aspublic class LinkedList private Node head; ...
– Stephen P
Mar 28 at 0:06
add a comment |
An aside:if(q.data==x && q!=null)
the conditions here should be flipped
– GBlodgett
Mar 27 at 23:16
1
Can you provide a more complete set of your code? Can you post what your entire LinkedList class looks like? YourremoveNode
method shouldn't be taking in the head node as a parameter. The head node should be an instance variable of the LinkedList class.
– JD D
Mar 27 at 23:56
YourLinkedList
class should keep track of its ownhead
, not take it as a parameter (to any of its methods) -- so it should start aspublic class LinkedList private Node head; ...
– Stephen P
Mar 28 at 0:06
An aside:
if(q.data==x && q!=null)
the conditions here should be flipped– GBlodgett
Mar 27 at 23:16
An aside:
if(q.data==x && q!=null)
the conditions here should be flipped– GBlodgett
Mar 27 at 23:16
1
1
Can you provide a more complete set of your code? Can you post what your entire LinkedList class looks like? Your
removeNode
method shouldn't be taking in the head node as a parameter. The head node should be an instance variable of the LinkedList class.– JD D
Mar 27 at 23:56
Can you provide a more complete set of your code? Can you post what your entire LinkedList class looks like? Your
removeNode
method shouldn't be taking in the head node as a parameter. The head node should be an instance variable of the LinkedList class.– JD D
Mar 27 at 23:56
Your
LinkedList
class should keep track of its own head
, not take it as a parameter (to any of its methods) -- so it should start as public class LinkedList private Node head; ...
– Stephen P
Mar 28 at 0:06
Your
LinkedList
class should keep track of its own head
, not take it as a parameter (to any of its methods) -- so it should start as public class LinkedList private Node head; ...
– Stephen P
Mar 28 at 0:06
add a comment |
5 Answers
5
active
oldest
votes
@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;
you should put theif (this.head.date == x)
logic before the loop... no reason to run that check every loop as it doesn't change
– JD D
Mar 28 at 0:46
that's true, but it would have to be double conditionif (this.head != null && this.head.data == x)
otherwise it would throw aNullPointerException
if theList
would be empty
– AP11
Mar 28 at 0:52
add a comment |
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();
add a comment |
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.
1
There is no such thing as buildList1() or printList() in the built in class. I think this is some sort of custom class for a school project or something of that nature.
– JD D
Mar 27 at 23:55
This was my own code
– user9179677
Mar 28 at 0:04
In which case I would suggest looking at the unlink(Node<E> x) source code. and manipulating it to fit your case.
– itstaito
Mar 28 at 0:08
This is obviously a personal/school project. Using built in libraries defeats the purpose.
– Christopher Schneider
Mar 28 at 2:40
add a comment |
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.
add a comment |
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));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387898%2fdelete-a-node-given-a-key-in-linked-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
@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;
you should put theif (this.head.date == x)
logic before the loop... no reason to run that check every loop as it doesn't change
– JD D
Mar 28 at 0:46
that's true, but it would have to be double conditionif (this.head != null && this.head.data == x)
otherwise it would throw aNullPointerException
if theList
would be empty
– AP11
Mar 28 at 0:52
add a comment |
@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;
you should put theif (this.head.date == x)
logic before the loop... no reason to run that check every loop as it doesn't change
– JD D
Mar 28 at 0:46
that's true, but it would have to be double conditionif (this.head != null && this.head.data == x)
otherwise it would throw aNullPointerException
if theList
would be empty
– AP11
Mar 28 at 0:52
add a comment |
@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;
@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;
edited Mar 28 at 0:54
answered Mar 28 at 0:35
AP11AP11
263 bronze badges
263 bronze badges
you should put theif (this.head.date == x)
logic before the loop... no reason to run that check every loop as it doesn't change
– JD D
Mar 28 at 0:46
that's true, but it would have to be double conditionif (this.head != null && this.head.data == x)
otherwise it would throw aNullPointerException
if theList
would be empty
– AP11
Mar 28 at 0:52
add a comment |
you should put theif (this.head.date == x)
logic before the loop... no reason to run that check every loop as it doesn't change
– JD D
Mar 28 at 0:46
that's true, but it would have to be double conditionif (this.head != null && this.head.data == x)
otherwise it would throw aNullPointerException
if theList
would be empty
– AP11
Mar 28 at 0:52
you should put the
if (this.head.date == x)
logic before the loop... no reason to run that check every loop as it doesn't change– JD D
Mar 28 at 0:46
you should put the
if (this.head.date == x)
logic before the loop... no reason to run that check every loop as it doesn't change– JD D
Mar 28 at 0:46
that's true, but it would have to be double condition
if (this.head != null && this.head.data == x)
otherwise it would throw a NullPointerException
if the List
would be empty– AP11
Mar 28 at 0:52
that's true, but it would have to be double condition
if (this.head != null && this.head.data == x)
otherwise it would throw a NullPointerException
if the List
would be empty– AP11
Mar 28 at 0:52
add a comment |
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();
add a comment |
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();
add a comment |
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();
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();
answered Mar 28 at 0:22
JD DJD D
1,54512 silver badges21 bronze badges
1,54512 silver badges21 bronze badges
add a comment |
add a comment |
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.
1
There is no such thing as buildList1() or printList() in the built in class. I think this is some sort of custom class for a school project or something of that nature.
– JD D
Mar 27 at 23:55
This was my own code
– user9179677
Mar 28 at 0:04
In which case I would suggest looking at the unlink(Node<E> x) source code. and manipulating it to fit your case.
– itstaito
Mar 28 at 0:08
This is obviously a personal/school project. Using built in libraries defeats the purpose.
– Christopher Schneider
Mar 28 at 2:40
add a comment |
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.
1
There is no such thing as buildList1() or printList() in the built in class. I think this is some sort of custom class for a school project or something of that nature.
– JD D
Mar 27 at 23:55
This was my own code
– user9179677
Mar 28 at 0:04
In which case I would suggest looking at the unlink(Node<E> x) source code. and manipulating it to fit your case.
– itstaito
Mar 28 at 0:08
This is obviously a personal/school project. Using built in libraries defeats the purpose.
– Christopher Schneider
Mar 28 at 2:40
add a comment |
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.
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.
answered Mar 27 at 23:49
itstaitoitstaito
525 bronze badges
525 bronze badges
1
There is no such thing as buildList1() or printList() in the built in class. I think this is some sort of custom class for a school project or something of that nature.
– JD D
Mar 27 at 23:55
This was my own code
– user9179677
Mar 28 at 0:04
In which case I would suggest looking at the unlink(Node<E> x) source code. and manipulating it to fit your case.
– itstaito
Mar 28 at 0:08
This is obviously a personal/school project. Using built in libraries defeats the purpose.
– Christopher Schneider
Mar 28 at 2:40
add a comment |
1
There is no such thing as buildList1() or printList() in the built in class. I think this is some sort of custom class for a school project or something of that nature.
– JD D
Mar 27 at 23:55
This was my own code
– user9179677
Mar 28 at 0:04
In which case I would suggest looking at the unlink(Node<E> x) source code. and manipulating it to fit your case.
– itstaito
Mar 28 at 0:08
This is obviously a personal/school project. Using built in libraries defeats the purpose.
– Christopher Schneider
Mar 28 at 2:40
1
1
There is no such thing as buildList1() or printList() in the built in class. I think this is some sort of custom class for a school project or something of that nature.
– JD D
Mar 27 at 23:55
There is no such thing as buildList1() or printList() in the built in class. I think this is some sort of custom class for a school project or something of that nature.
– JD D
Mar 27 at 23:55
This was my own code
– user9179677
Mar 28 at 0:04
This was my own code
– user9179677
Mar 28 at 0:04
In which case I would suggest looking at the unlink(Node<E> x) source code. and manipulating it to fit your case.
– itstaito
Mar 28 at 0:08
In which case I would suggest looking at the unlink(Node<E> x) source code. and manipulating it to fit your case.
– itstaito
Mar 28 at 0:08
This is obviously a personal/school project. Using built in libraries defeats the purpose.
– Christopher Schneider
Mar 28 at 2:40
This is obviously a personal/school project. Using built in libraries defeats the purpose.
– Christopher Schneider
Mar 28 at 2:40
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 27 at 23:58
Bohemian♦Bohemian
312k68 gold badges444 silver badges587 bronze badges
312k68 gold badges444 silver badges587 bronze badges
add a comment |
add a comment |
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));
add a comment |
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));
add a comment |
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));
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));
answered Mar 28 at 0:41
SK -SK -
1399 bronze badges
1399 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387898%2fdelete-a-node-given-a-key-in-linked-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
An aside:
if(q.data==x && q!=null)
the conditions here should be flipped– GBlodgett
Mar 27 at 23:16
1
Can you provide a more complete set of your code? Can you post what your entire LinkedList class looks like? Your
removeNode
method shouldn't be taking in the head node as a parameter. The head node should be an instance variable of the LinkedList class.– JD D
Mar 27 at 23:56
Your
LinkedList
class should keep track of its ownhead
, not take it as a parameter (to any of its methods) -- so it should start aspublic class LinkedList private Node head; ...
– Stephen P
Mar 28 at 0:06