Duno
Duno

Reputation: 47

How to add a new node before the head of a linked list

I would like to ask: how to add a new node before the head of a linked list: Here is my code:

   //   Definition for singly-linked list.
public class ListNode {
    int val; 
    ListNode next; 
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    
    public void addAfter(ListNode thisnode, int x) {
        ListNode newNode = new ListNode(x);
        if(thisnode == null) {
            //add before the head
            newNode.next = this;
            
        }//wrong here
        else {
            ListNode currentNext = thisnode.next;
            thisnode.next = newNode;
            newNode.next = currentNext;
        }
        return;
    }//done addAfter this node
    

    
}

For example, with the input list 2 100 300 800, l.addAfter(null,500); the output should be 500 2 100 300 800 but my output is still 2 100 300 800. Thank you.

Upvotes: 0

Views: 255

Answers (4)

Sayef Reyadh
Sayef Reyadh

Reputation: 41

You can always make one head that is constant and add all the new elements after it. Example:

Head - Link1 - Link2 - Link3

Whenever you want to add newLink you can just add it in this manner.

Head - newLink - Link1 - Link2 - Link3

In this way you head information is never lost and it reduce the chances of losing your entire list.

You can explore my codebase here which implements it.

https://github.com/SayefReyadh/tutorials/tree/master/LinkedList-Bangla-Tutorial/LinkedListExample/src/linkedlistexample

The method is implemented as insertFront in LinkedList.java

Upvotes: 0

user14215102
user14215102

Reputation:

Inserting before head would change the value of head. Which you can't do from inside the method.

public ListNode addAfter(ListNode thisnode, int x) {
    ListNode newNode = new ListNode(x);
    if(thisnode == null) {
        //add before the head
        newNode.next = this;
        return newNode;    
     } else {
        ListNode currentNext = thisnode.next;
        thisnode.next = newNode;
        newNode.next = currentNext;
     }
     return this;
}

And the caller would call it like l = l.addAfter(null,500);

Upvotes: 1

vaibhavsahu
vaibhavsahu

Reputation: 682

// Node Class

 public class SinglyListNode {
        int val;
        SinglyListNode next;
        SinglyListNode(int x) { val = x; }
    }

//Singly Linked List class

public class SinglyLinkedList {
        private SinglyListNode head;
        public SinglyLinkedList() {
            head = null;
        }

        // add at beginning
        public void addAtBeginning(int value){
            SinglyListNode node = new SinglyListNode(value);
            if (head != null) {
                node.next = head;
            }
            head = node;
        }

        // print list
        public void printList(){
            System.out.println("printing linked list");
            SinglyListNode curr = head;
            while (curr != null){
                System.out.println(curr.val);
                curr = curr.next;
            }
        }

}

//main method

public static void main(String[] args) {
    SinglyLinkedList linkedList = new SinglyLinkedList();
    linkedList.addAtBeginning(5);
    linkedList.addAtBeginning(6);
    linkedList.addAtBeginning(7);
    linkedList.printList();
}

Upvotes: 1

InSaNiTy
InSaNiTy

Reputation: 150

First of all, make your code clear, readable and consistent. Here is what you asked for:

public class Node {

    private int value;
    private Node next;

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

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

public class ListNode {

    private Node head;

    public ListNode(Node head) {
        this.head = head;
    }

    public Node getHead(){
        return head;
    }

    // Here is your function
    public void insertBeforeHead(int value) {
        Node node = new Node(value);

        node.setNext(head); 
        head = node;
    }
}

Upvotes: 1

Related Questions