Insert a Node at the Tail of a Linked List

Sort by

recency

|

1768 Discussions

|

  • + 0 comments

    Java:

        static SinglyLinkedListNode insertNodeAtTail(SinglyLinkedListNode head, int data) {
            if(head != null){
                if(head.next != null) {
                    SinglyLinkedListNode node = head.next;
                    while(node != null) { // Iterate trough list
                        if(node.next == null) {
                            node.next = new SinglyLinkedListNode(data);
                            break;
                        }
                        node = node.next;
                    }
                } else {
                    head.next = new SinglyLinkedListNode(data);
                }
            }else {
                head = new SinglyLinkedListNode(data);
            }
            return head;
        }
    
  • + 0 comments

    Sorry: TabError: inconsistent use of tabs and spaces in indentation (Solution.py, line 53)

    Seriously this is a very poor site.

  • + 0 comments

    I'm trying to run my code but it seems to be getting None as the object for the linkedlist. I'm pretty sure there is something wrong with the main function for the output file

  • + 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?