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

Osiris

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.

dratenik

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add a new node to a linked list alphabetically

Linked List implementation: why cannot move to head.next before creating new node

How add node before doubly linked list c++

The head node in a linked list

How to add a new node at the start of a doubly linked list?

Head of linked list always null after adding new node

How do I insert a new node before first node of a doubly-linked list?

Making of linked list by head and node

Detect the head node of linked list

Adding a node to the head of the linked list

Not able to add to the head of a Linked List

How to add multiple nodes to linked list in C with no head/tail pointer?

How to add a node at the beginning of a single linked list?

linked-list: why extend a single-node list by adding the new node to head->next, but not head=head->next and then adding to head

Updating the head node automatically after inserting new node at the beginning of the linked list in java

How to use function prototype on Leetcode to add a new node at the begining of linked list?

Keeping track of the head node in Linked List in C

Assigning NULL to the head node in a linked list in C

Declaration of Head/First node in a Linked list

how to add a node after a given node in circular linked list?

Python Linked list new node

How head node's nextRef got updated while updating tail node's nextRef in Single Linked List

Add end node to linked list

Linked list, add node to end

Failed to add a node to linked list

How to insert a new node at any given position in linked list?

Linked list error: syntax error : missing ';' before identifier 'head'

How to clone a linked list with a head/tail implementation?

How to make linked list with localy declared head?