Linked list head double pointer passing

前端 未结 5 2207
一生所求
一生所求 2020-12-17 05:21

I have seen this in some book/ tutorial.

When you pass in the head pointer (of linked list) into a function, you need to pass it as a double pointer.

For eg:

5条回答
  •  醉梦人生
    2020-12-17 05:52

    The reason for the double indirection is so nReverse can modify the caller's pointer, since after reversing the list, the head of the list is now a different node.

    In the second version, you are modifying the copy of head that is local to the function, so the caller still has a reference to the old head node, which is now the tail.

提交回复
热议问题