Question: I've just recently gotten into linked lists and I'm adding my own "add to end" function. 

```
void insert_at_end(Node** head, Node* node)
{
    Node* temp;

    temp = *head;

    if (temp == NULL) // linked list is empty
    {
        *head = node;
    }
    else
    {
        while (temp->pNext != NULL)
        {
            temp = temp->pNext;
        }
    temp->pNext = node;
    }
}

```

Originally I was doing `temp != NULL` instead of `temp->pNext != NULL` because I thought that way it would take me to the very LAST node, since it stops looping when temp still has data before it reaches NULL. Wouldn't `temp->pNext != NULL` stop at the 2nd to last node? Since it stops looping when it realizes that the last nodes next pointer is NULL, it does not travel to that node?

Thanks guys, If I need to clear anything up from that word vomit let me know.

Answer: 
> 
> Wouldn't `temp->pNext != NULL` stop at the 2nd to last node?
> 
> 
> 

No. Here is the pictorial representation for ease of understanding.

With `temp != NULL`, `temp` will be pointing to `NULL`:

```
 +---+    +---+    +---+
 | 1 |--->| 2 |--->| 3 |---> NULL
 +---+    +---+    +---+      ^
                              |
                             temp

```

As you see `temp` is iterating until it becomes `NULL`, at which point you cannot attach anything to the final node because you're past it.

---

With `temp->pNext != NULL`, `temp` will be pointing to last node.

```
 +---+    +---+    +---+
 | 1 |--->| 2 |--->| 3 |---> NULL
 +---+    +---+    +---+
                     ^
                     |
                    temp

```

Here `temp` will iterate till its next node is `NULL`. That means you're pointing to the final node and you can use that pointer to adjust that node to point to a new one with `temp->pNext = node`:

```
 +---+    +---+    +---+    +---+
 | 1 |--->| 2 |--->| 3 |--->| 4 |---> NULL
 +---+    +---+    +---+    +---+
                     ^
                     |
                    temp

```

---

As an aside, you *may* also want to add the extra safety of ensuring the node you're given points to `NULL`, on the off chance the caller may forget to do that. That's as simple as adding this line to the start of the function:

```
node->pNext = NULL;

```
Let's make some analogy.

* Each node is a station.
* `NULL` is your stop station.
* `while (temp->pNext != NULL) { temp = temp->pNext; }` means drive and visit each station until reach the stop station.
* When you reach the stop station, an additional station is needed to be added by `temp->pNext = node;`

Now the former stop station followed by the new added stop station(implicitly the new `NULL`) *is not* a stop station, but the penultimate station to be visited.