Linked list head double pointer passing

前端 未结 5 2304
一生所求
一生所求 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 06:08

    The reason for having double pointer passing (the first example) is that you want to change the head of the list. Since you are reversing the list, the head should point to the last element of the list after you have done the reversing.

    digit* list; 
    // initialize list
    nReverse(&list); 
    // now list is pointing to the last element of the chain (and not the first)
    

    If you don't use double pointers, then list will still point to the original first element which next now points to NULL because its the last element after the reversing. So you loose all your other elements.

提交回复
热议问题