Insert a Node at the Tail of a Linked List

  • + 6 comments

    Java implementation :

    if (head == null){
            head = new Node();
            head.data = data;
        }
        else {
            Node node = head;
            while (node.next != null){
                node = node.next;
            }
            node.next = new Node();
            node.next.data = data;
        }
        return head;