LinkedList question




Comment hidden because of low score. Click to expand.
0
of 0 vote

Consider singly linked list 1 --> 2 --> 3 --> 4 --> 5 --> 6 --> NULL.
The 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.

- Bhaskar September 08, 2014 | Flag Reply




Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More