I am trying to create a link list. I wanted to know how these two codes are different? Code1: if (start == NULL) { start = newnode; } else { temp = start; while(temp != NULL) { temp = temp->next; }

temp = newnode;

}

Code2: if (start == NULL) { start = newnode; } else { temp = start; while(temp->next != NULL) { temp = temp->next; }

temp->next = newnode; }

}

Please fix this, i can't understand the code!!!

I don't think Code1 should work. You say

`while(temp != NULL) temp = temp->next`

. So when you come out of the loop temp is NULL. Now you try and assign newnode to temp which is pointless.Code2 on the other hand does what you want i.e. insertion. You say

`while(temp->next != NULL) temp = temp->next`

. So when you break out of the loop temp points to the last element in your list(since temp->next is NULL). Now you say temp->next = newnode which makes sense.code one won't at all work.! flaw(s): 1.In the while loop, condition is temp!=NULL i.e. it will iterate till Temp is not NULL.Now, while insertion ideally you want to travel to the last node and NULL node.Question is how to find the last node? The last node is the node that has it's next pointer pointing to NULL. Therefore the condition: while(temp->next != NULL) is correct. 2.After you reach the last node, the newly created node will replace the next pointer of last node from NULL to itself. Therefore the condition of temp->next = new_node; is viable and not temp=newnode!

Hope I was sound enough. Happy Coding!~