首页 > 解决方案 > 链表添加新的根元素

问题描述

我正在尝试将一个新的根元素添加到我的链表中。我已经知道正确的答案,但我不明白为什么有一个指向当前根元素的双指针。这是正确的代码:

void elementAsRoot(Element **oldRoot, Element *newRoot){
  newRoot -> next = *oldRoot;
  *oldRoot = newRoot;
}

标签: clistlinked-listnodes

解决方案


我很确定你可以删除双指针,因为你最终会取消引用双指针。

void elementAsRoot(Element *oldRoot, Element *newRoot) {
  newRoot->next = oldRoot;
  oldRoot = newRoot;
}

推荐阅读