1

I wanted to learn about linked list, so I tried to implement one from scratch. The logic makes sense, however when adding a value to the linkedlist it overwrites the values. So if I added the ones 1 -> 2 -> 3 -> 4 -> 5, but I wont get these values when I print the content of my linked list...

Any idea how to fix the solution?

public class LinkedListM {
    Node head;

    class Node {
        Node next;
        int value;

        Node(int value) {
            this.value = value;
            this.next = null;
        }
    }
    
    public void add(int value) {
        // System.out.println("inserting: " + value);
        Node node = new Node(value);
        if (head == null) {
            head = node;
        }
        else {
            while (head.next != null) {
                // System.out.println(head.value + " -> " + head.next.value);
                head = head.next;
            }
            head.next = node;
        }
    }

    public void remove(int value) {
        LinkedListM list = new LinkedListM();
        Node dummy = new Node(0);
        dummy.next = head;

        while (dummy.next != null) {
            if (dummy.next.value == value) {
                if (dummy.next.next == null) {
                    dummy.next = null;
                }
                else {
                    dummy.next = dummy.next.next;
                }
            }
            dummy = dummy.next;
        }
    }
}

Here is me testing the linked list:


public class p {
    
    public static void main(String[] args) {
        LinkedListM ll = new LinkedListM();
        ll.add(1);
        ll.add(2);
        ll.add(3);
        ll.add(4);
        ll.add(5);

        // System.out.println(ll);

        while(ll.head.next != null) {
            System.out.print(ll.head.value + " -> ");
            ll.head = ll.head.next;
        }
        System.out.println();
    }
}

1
  • In the case where head != null you are modifying head. You need a local variable. Commented Oct 2, 2022 at 3:43

1 Answer 1

1

first of all, writing ll.head = ll.head.next; is a bad practice because you are changing the head of the linked list, instead create a dummy Node and use it to iterate all over the array, so instead of :

while(ll.head.next != null) {
    System.out.print(ll.head.value + " -> ");
    ll.head = ll.head.next;
}
System.out.println();

you should write:

LinkedListM.Node dummy = ll.head;
while(dummy != null) {
    System.out.print(dummy.value + " -> ");
    dummy = dummy.next;
}
System.out.println();

again, in the code of inserting nodes, you are writing:

head = head.next;

which will change the head of the linked list, instead create a dummy node and use it to insert into the linked list or make a tail Node for the linked list like:

tail.next = node;
tail = node;

instead of

while (head.next != null) {
    // System.out.println(head.value + " -> " + head.next.value);
    head = head.next;
}
head.next = node;

apply the same concept for your function called remove as it has some logical errors.

this is the code edited:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {
        LinkedListM ll = new LinkedListM();
        ll.add(1);
        ll.add(2);
        ll.add(3);
        ll.add(4);
        ll.add(5);



        // System.out.println(ll);
        LinkedListM.Node dummy = ll.head;
        while(dummy != null) {
            System.out.print(dummy.value + " -> ");
            dummy = dummy.next;
        }
        System.out.println();

        System.out.println("removing value 3");
        ll.remove(3);

        dummy = ll.head;
        while(dummy != null) {
            System.out.print(dummy.value + " -> ");
            dummy = dummy.next;
        }
        System.out.println();

    }

}
 class LinkedListM {
    Node head;
    Node tail;

    class Node {
        Node next;
        int value;

        Node(int value) {
            this.value = value;
            this.next = null;
        }
    }

    public void add(int value) {
        // System.out.println("inserting: " + value);
        Node node = new Node(value);
        if (head == null) {
            head = node;
            tail = node;
        }
        else
        {
            tail.next = node;
            tail = node;
        }
    }

    public void remove(int value) {
        Node dummy = head;

        if (head.value == value)
        {
            head = head.next;
        }
        else
        {
            while (dummy.next != null && dummy.next.value != value)
                dummy = dummy.next;

            if (dummy.next != null)
                dummy.next = dummy.next.next;

        }
    }
}

and this is the output:

1 -> 2 -> 3 -> 4 -> 5 -> 
removing value 3
1 -> 2 -> 4 -> 5 ->
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.