LinkedList question
1 Answer
LinkedList question
| Flag | PURGE
The code below is from the book.
public static void deleteDups(LinkedListl\lode n) {
......(many lines)
while(n.next != null)
n = n.next;
.....
}
In this situation, LinkedListNode is a class,
so if you give LikedListNode n to this method, this method receive the node n rather than the copy of n.
when using n = n.next, it change the head of the LinkedList, am I right?
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Consider singly linked list 1 --> 2 --> 3 --> 4 --> 5 --> 6 --> NULL.
- Bhaskar September 08, 2014The head and n is pointing to 1.
Remember n is a reference to the head of the linked list and not the head itself.
By moving n along the linked list (say you are at 4 now) you are moving the reference along the linked list, the head essentially stays at 1, its just that you have lost the reference to head (1) because you have moved your only reference (n) to 4.
The reason why you are being able to move the reference along the linked list is because every LinkedListNode object n is referencing to has a pointer (reference in OO) to the next object. You continue to move along the linked list until you run out of references which is encounter NULL.
Having another reference (n1) pointing to the head saves the day.