In: Computer Science
1- Given following data structure of Single linked list :
class ListNode
{ int item ;
ListNode next ;
….
}
Choose the correct answer :
1- reference++ ;
2- reference = next ;
3- reference+= next ;
1- (p == null)
2- (p.next == null)
4- (p.item == 0)
5- None of the above.
1- n == m
2- n.item == m.item
4- None of the above
First Question:
Fourth option is the correct answer.
The following statement changes reference so that it refers to the next node:
reference = reference.next ;
First option is incorrect as it will not change reference so that it refers to the next node.
Second option is incorrect as it will not change reference so that it refers to the next node.
Third option is incorrect as it will not change reference so that it refers to the next node.
Second Question:
Second option is the correct answer.
When 'p' is referring to the last node of the list, then the 'next' pointer must be null as there is no more node. So, it will be:
(p.next == null)
First option is incorrect as this Boolean expression will not be true when 'p' refers to the last node of the list.
Third option is incorrect as this Boolean expression will not be true when 'p' refers to the last node of the list.
Fourth option is incorrect as this Boolean expression will not be true when 'p' refers to the last node of the list.
Fifth option is incorrect as the second option is true.
Third Question:
Second option is the correct answer.
The following Boolean expression indicates that the items in two nodes (n and m) are the same when both are not null:
n.item == m.item
First option is incorrect as this following Boolean expression does not indicate that the items in two nodes (n and m) are the same when both are not null.
Third option is incorrect as this following Boolean expression does not indicate that the items in two nodes (n and m) are the same when both are not null.
Fourth option is incorrect as the second option is true.
Please comment in case of any doubt.
Please upvote if this helps.