-2

My question comes up when I'm doing some linked list practice. My task here is to combine two linked lists into one, so I will first need to get the address of the last node of the first linked list and assign the first node of the second list to the next of the last node in my first linked list, namely

list1->last->next = list2->first;

So we all know that, list1.last->next is NULL, and let's say what if I do:

Node * lastNext = list1->last->next;
lastNext = list2->first;

Will this work?

Thanks guys!

Hang
  • 355
  • 3
  • 19
  • Note: contrary to what the edit history says, I did not vote to close this question as a duplicate. It isn't, at least not of the question cited. I voted to close it as 'unclear what you're asking', which it is. – user207421 Dec 08 '17 at 08:59
  • Yes, after drawing several graphs I realized what you said in your answer. A pointer equal doesn't mean they become the same thing, even though I knew it at first, but it seems I haven't fully mastered the full concept so I could still make mistakes sometimes. This problem has nothing to do with NULL. Even if list1->last->next is not NULL, what I am doing here is just changing the reference of lastNext from list1->last->next to list2->first and not affect the list in any way. No matter what, I always have to assign the head to the next pointer of the last node in the first list. – Hang Dec 08 '17 at 09:26

1 Answers1

2

No, it will not work, and the reason why has nothing to do with NULL. It has to do with the fact that all you're doing here is changing the value of a local pointer variable. You aren't affecting the list in any way. Specifically, you aren't changing the value of list->last->next.

If lastNext was a reference:

Node *&lastNext = list1->last->next;

it would be a different matter. But it isn't. And NULL would still not come into play.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks EJP! Could you elaborate "all you're doing here is changing the value of a local pointer variable"? Had I changed the value of list1->last->next? And to what? Thank you! :) – Hang Dec 08 '17 at 08:04
  • In the special cases, you end up dereferncing `NULL` right? EDIT: I misread the question, my bad. – Yashas Dec 08 '17 at 08:09
  • @Hang I don't know why you're asking me questions I've already answered. 'Specifically, you aren't changing the value of `list->last->next`. And surely you know what `lastNext = list2->first` changes the value of `lastNext` to? – user207421 Dec 08 '17 at 08:55
  • @Yashas There is no deferencing here at all of anything, let alone of NULL. – user207421 Dec 08 '17 at 08:56
  • @EJP Oh yes my bad... but you said "all you're doing here is changing the value of a local pointer variable". I don't know which value had I changed? All I could see that if there's a value that's changed then it would be "list1->last->next"? – Hang Dec 08 '17 at 09:00
  • @Hang You changed, and I quote, again, 'the value *of a local pointer variable*'. What's unclear about that? In other words, the value of `lastNext`. Is this such a mystery? – user207421 Dec 08 '17 at 09:00
  • @EJP Which local pointer variable? And changed to what value? Do you mean I changed "list2->first" to NULL? Thanks – Hang Dec 08 '17 at 09:03
  • @EJP Sorry!!! I just saw you edited a bit in your last comment by adding "And surely you know what lastNext = list2->first changes the value of lastNext to?" so you mean I changed lastNext to list2->first right? – Hang Dec 08 '17 at 09:07
  • @EJP I understand now... sorry I went full retard a bit. Appreciate your help! – Hang Dec 08 '17 at 09:12