Swapping Nodes on a single linked list

前端 未结 8 1108
说谎
说谎 2020-12-03 16:11

I am trying to make a swapNode function that can take any two nodes and swap them. I\'ve made an algorithm that works if they\'re at least 2 nodes away, but I c

8条回答
  •  感动是毒
    2020-12-03 16:31

    Rule of thumb: "Always separate data from pointers and never swap pointers, only the data!". Make swap explicit without using memcpy(), thus you can avoid alignment problems. It causes no performance penalty in terms of algorithmic complexity, but makes your code more readable and safe.

提交回复
热议问题