Insert a Node at the Tail of a Linked List

Sort by

recency

|

1765 Discussions

|

  • + 0 comments

    Python

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

    do i need to write the main function?

  • + 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;
    
    														}