Insertion at the tail of Linked List

Pattern: Traversal and dummy nodes

easy

Walk to the last node and point it at the new node. An empty list has no last node, so the new node is the whole list.

The problem

Given the head of a singly linked list and a value X, add a new node holding X at the end and return the head. For example, 1 → 2 with X = 3 becomes 1 → 2 → 3, and an empty list with X = 0 becomes 0.

The function name insertAtTail matches the takeUforward practice page. Use the link above for the exact statement and limits.

Think first

  • Which node's pointer must change?
  • How do you know you are on the last node?
  • What does an empty list need?

Hints

    Brute force, and why it fails

    There is no shortcut without extra information: the walk to the end is O(n). Keeping a tail pointer (as queues do) would make it O(1), but the input gives only the head.

    The key insight

    Insertion is about the node before the spot. For the end, that is the last node, found by walking until next is null.

    Dry run

    10cur
    21 

    The list is 1 → 2 and X = 3. cur starts at the head.

    Step 1 of 3

    The plan

    node = new node(X)
    if head is null: return node
    cur = head
    while cur.next is not null: cur = cur.next
    cur.next = node
    return head
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    One walk to the end.

    Edge cases

    • Empty list: the result is just X.
    • One node: the new node becomes its next.
    • Long lists: the walk is the whole cost.
    I've built my logic, show the code

    Java

    class Solution {
        public ListNode insertAtTail(ListNode head, int X) {
            ListNode node = new ListNode(X);
            if (head == null) return node;          // an empty list: the new node is the whole list
            ListNode cur = head;
            while (cur.next != null) cur = cur.next;   // walk to the last node
            cur.next = node;
            return head;
        }
    }

    C++

    class Solution {
    public:
        ListNode* insertAtTail(ListNode* head, int X) {
            ListNode* node = new ListNode(X);
            if (head == NULL) return node;          // an empty list: the new node is the whole list
            ListNode* cur = head;
            while (cur->next != NULL) cur = cur->next;   // walk to the last node
            cur->next = node;
            return head;
        }
    };

    Python

    from lib import ListNode
    
    class Solution:
        def insertAtTail(self, head: ListNode, X: int) -> ListNode:
            node = ListNode(X)
            if head is None:
                return node                         # an empty list: the new node is the whole list
            cur = head
            while cur.next is not None:             # walk to the last node
                cur = cur.next
            cur.next = node
            return head

    Common mistakes

    • Walking while cur != null, which ends with cur null and nothing to attach to.
    • Forgetting the empty-list case, which reads null.next.
    • Returning the new node instead of the head for a non-empty list.

    Similar problems