at first i want to traverse two times, first to get the odd nodes then even nodes, then combine them together。after i leaved the library, i am thinking about it when my brain get a slot from cooking and walking dog. right before i was going to bed, i got an idea, using two pointer to get odd nodes and even nodes at the same time. i am so glad i can't wait for tomorrow. i am tired but i still begin to code.
i pointer odd and even to the first two nodes, so i suppose i should start loop from the third and fourth nodes, the code looks kind of weird as
event.next = event.next.next
and when it comes to the fifth node, it can't meet the loop condition. so i add dummy nodes to loop from the first two nodes, then it said Memory Limit Exceeded, at first i suppose it was caused by the space complexity, i used two nodes, but chatgtp said with two node, the space complexity is still o(1), the root cause is cycle in the linked list after i combine two links.
at last i choose another clumsy approach, i was so close to the right approach, just need more rounds try.
actually i really don't need dummy node, since the head doesn't change. it's always the head of odd nodes, the list.
Top comments (0)