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

0% completed

Vote For New Content
Can someone explain the logic to the rearange to produce the linkedlist in requi...

Justin Cook

Mar 6, 2022

Can someone explain the logic to the rearange to produce the linkedlist in required order part? ListNode temp = headFirstHalf.next; headFirstHalf.next = headSecondHalf; headFirstHalf = temp;

temp = headSecondHalf.next; headSecondHalf.next = headFirstHalf; headSecondHalf = temp;

I cant wrap my brain around it. Maybe I don't quite understand how each of these change the actual head. Are all of these working directly on ListNode Head and not copies? I'm using java.

1

0

Comments
Comments
A
Angel Morales4 years ago

Yeah they aren't copies. They are all working on the same Linked List

F
fellainthewagon a year ago

I had the same problem. head is holding all list, while headFirstHalf simply slides like a left-hand finger across the piano keys. Same with headSecondHalf - they are just pointers variables that holding target nodes

On this page