Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Prior to exiting the while loop that rearranges the LinkedList, would the last ...

Eric C

Apr 13, 2022

Prior to exiting the while loop that rearranges the LinkedList, would the last node be pointing to itself? For instance, if we had 2 -> 4 -> 6 -> 8 -> 10 -> 12 -> null, and it's rearranged to 2 -> 12 -> 4 -> 10 -> 6 -> 8 , then before the lines (Java): if (headFirstHalf != null) headFirstHalf.next = null; would the 8 node be pointing to itself? I'm having a tough time visualizing this part.

1

1

Comments
Comments
E
Eric C4 years ago

Also, when we reverse the second half of the LinkedList, would it resulting list be: 2 -> 4 -> 6 -> 8

B
Brandon Figueredo3 years ago

Getting the same result too. The last node will be pointing to else since headFirstHalf and headSecondHalf can become to same node.

B
Brandon Figueredo3 years ago

pointing to *itself

F
fellainthewagon a year ago

It is pointing to itself, but on picture a null between them which is confusing:

...->6->null<-8<-...

On this page