Insert a Node at the Tail of a Linked List

Sort by

recency

|

1763 Discussions

|

  • + 0 comments

    Typescript. "Linked list" is a bit of a stretch.

        inputLines.forEach((x, i) => {
            if (i) {
                console.log(x);
            }
        })
    
  • + 0 comments

    The example code doesn't exist in Swift for some reason.

  • + 0 comments

    Here's a solution in C#

    static SinglyLinkedListNode insertNodeAtTail(SinglyLinkedListNode head, int data) {
            
            SinglyLinkedListNode node = new SinglyLinkedListNode(data);
            
            if(head == null)
                head = node;
    
            else
            {
                var currentNode = head;
                
                while(currentNode.next != null){
                    currentNode = currentNode.next;
                }
                
                currentNode.next = node;
            }
            
            return head;
    
    														}
    
  • + 0 comments

    // cpp code SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) { //ll doestnt exist

        if(head==NULL){
            head= new SinglyLinkedListNode(data);
            return head;
        }
        //ll already exist
        else{
         SinglyLinkedListNode* temp=head;
         while(temp->next!=NULL){
            temp=temp->next;
         }
         temp->next=new SinglyLinkedListNode(data);
         return head;
        }
    

    }

  • + 1 comment

    Here is a simple function in C language, that inserts a node at the tail of the list:

    SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {
        SinglyLinkedListNode *newNode = (SinglyLinkedListNode *)malloc(sizeof(SinglyLinkedListNode));
        
        if(head == NULL){
            head = newNode;
            newNode->data = data;
            newNode->next = NULL;
            return head;
        }
        
        SinglyLinkedListNode *temp = head;
        while(temp->next != NULL) {
            temp = temp->next;
        }
        newNode ->data = data;
        temp->next = newNode;
        temp = newNode;
        newNode->next = NULL;
        
        return head;
    
    }