You are viewing a single comment's thread. Return to all comments →
Java Solution: Maintain tail node while traversing from head and at the end assign new node.
static SinglyLinkedListNode insertNodeAtTail(SinglyLinkedListNode head, int data) { SinglyLinkedListNode nn = new SinglyLinkedListNode(data); if(head == null) { return nn; } else { SinglyLinkedListNode temp = head, prev = null; while(temp != null) { prev = temp; temp = temp.next; } prev.next = nn; return head; } }
Insert a Node at the Tail of a Linked List
You are viewing a single comment's thread. Return to all comments →
Java Solution: Maintain tail node while traversing from head and at the end assign new node.