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;








3















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();













share|improve this question


























  • 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 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

















3















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();













share|improve this question


























  • 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 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













3












3








3








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();













share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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? 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

















  • 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 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
















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












5 Answers
5






active

oldest

votes


















1















@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;






share|improve this answer



























  • 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


















1















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();








share|improve this answer
































    0















    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.






    share|improve this answer




















    • 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


















    0















    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.






    share|improve this answer
































      0















      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));





      share|improve this answer



























        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
        );



        );













        draft saved

        draft discarded


















        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









        1















        @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;






        share|improve this answer



























        • 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















        1















        @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;






        share|improve this answer



























        • 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













        1














        1










        1









        @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;






        share|improve this answer















        @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;







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 28 at 0:54

























        answered Mar 28 at 0:35









        AP11AP11

        263 bronze badges




        263 bronze badges















        • 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

















        • 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
















        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













        1















        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();








        share|improve this answer





























          1















          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();








          share|improve this answer



























            1














            1










            1









            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();








            share|improve this answer













            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();









            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 28 at 0:22









            JD DJD D

            1,54512 silver badges21 bronze badges




            1,54512 silver badges21 bronze badges
























                0















                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.






                share|improve this answer




















                • 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















                0















                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.






                share|improve this answer




















                • 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













                0














                0










                0









                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                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












                • 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











                0















                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.






                share|improve this answer





























                  0















                  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.






                  share|improve this answer



























                    0














                    0










                    0









                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 27 at 23:58









                    BohemianBohemian

                    312k68 gold badges444 silver badges587 bronze badges




                    312k68 gold badges444 silver badges587 bronze badges
























                        0















                        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));





                        share|improve this answer





























                          0















                          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));





                          share|improve this answer



























                            0














                            0










                            0









                            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));





                            share|improve this answer













                            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));






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 28 at 0:41









                            SK -SK -

                            1399 bronze badges




                            1399 bronze badges






























                                draft saved

                                draft discarded
















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현