Insert a Node at the Tail of a Linked List

Sort by

recency

|

1756 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch this video explanation here : https://youtu.be/zwvWP4YmsoM

    SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {
        SinglyLinkedListNode* newNode = new SinglyLinkedListNode(data);
        if(head != nullptr) {
            SinglyLinkedListNode* curr = head;
            while(curr->next) curr = curr->next;
            curr->next = newNode;
            return head;
        }
        return newNode;
    }
    
  • + 0 comments

    A Python solution:

    def insertNodeAtTail(head, data):
        new_node = SinglyLinkedListNode(data)
        if head is not None:
            current = head
            while current.next:
                current = current.next
            current.next = new_node
            return head
        return new_node
    
  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        LinkedList<Integer> list = new LinkedList<>();
        int n = Integer.parseInt(reader.readLine());
    
        for(int i = 0; i<n; i++){
            int value = Integer.parseInt(reader.readLine());
            list.add(value);
        }
    
        list.printValues();
    }
    

    }

    class Node{ private T value; private Node nextNode;

    public Node(T value){
        this.value = value;
        this.nextNode = null;
    }
    
    public Node<T> getNextNode(){
        return nextNode;
    }
    
    public void setNextNode(Node<T> nextNode){
        this.nextNode = nextNode;
    }
    
    public T getValue(){
        return value;
    }
    

    }

    class LinkedList{ Node head, tail;

    public LinkedList(){
        this.head = null;
        this.tail = null;
    }
    
    public void add(T value){
        Node<T> newNode = new Node<>(value);
        if(head == null){
            head = tail = newNode;
        }else{
            tail.setNextNode(newNode);
            tail = newNode;
        }
    }
    
    public void printValues(){
        Node<T> current = head;
        StringBuilder ret = new StringBuilder();
        while(current != null){
            ret.append(current.getValue()).append('\n');
            current = current.getNextNode();
        }
        System.out.println(ret.toString());
    }
    

    }

  • + 0 comments

    Hello developers now i am solve the problem with javascript

    For Just understanding i am not giving the full answer to you i will give you the hints okh bro condition if the head is empty

    // make the new node => let newNode =new singlyLinkedList(data); let currentNode=head; loop and find the prevous node of the tail currentNode=currentNode.next

    currentNode.next=newNode

  • + 0 comments

    There is a compilation error in the provided Python code. Please fix