实现链表节点插入
插入节点到链表头部时,先把新节点的next指向当前头节点,然后更新头指针指向新节点;插入尾部时需要遍历链表找到最后一个节点,将其next指向新节点。示例:
// 插入头部
newNode->next = head;
head = newNode;
// 插入尾部
Node* temp = head;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = NULL;