You are viewing a single comment's thread. Return to all comments →
Better to track the last node rather than constantly looping over the list.
static SinglyLinkedListNode last = null; static SinglyLinkedListNode insertNodeAtTail(SinglyLinkedListNode head, int data) { SinglyLinkedListNode newNode = new SinglyLinkedListNode(data); if (head == null) { head = newNode; last = newNode; } else { last.next = newNode; last = newNode; } return head; }
Seems like cookies are disabled on this browser, please enable them to open this website
Insert a Node at the Tail of a Linked List
You are viewing a single comment's thread. Return to all comments →
Better to track the last node rather than constantly looping over the list.