i used two way to solve the problem. the first way is to consider the situation of third node and forth node, to put the first node point to fourth node.it looks so complicated. in second way, i didn't care about the third and fourth node, just set a pre pointer to update it point to swapped node in next round. it's more clean but still a little bit complicated. i did following things:
- preserve the pointer for next round
- handle the case if it's the end of list
- swap
- handle the case if it's head.(i didn't use dummy node)
- hande pre pointer.
in the solution ,the code is more clean cause they only consider about the case when first and second node both exists, cause if the second one doesn't exist, there's no need to reverse the link. they also put the first node point third node and i only put it pointe to none when it's the last one.someone even swap the three nodes in one line!
i also learned to initiate dummy node in one line.
Top comments (0)