void Print(Node *head) {
head = head->next; // 跳過首節點!
while (head) { printf("%d ", head->data); head = head->next; }
}應改為:
void Print(Node *head) {
while (head) { printf("%d ", head->data); head = head->next; }
}insert 中 pr = malloc(...) 分配新節點,但後續邏輯將 pr 掛入鏈表時未處理 head 更新返回值正確性,建議統一返回新頭指針並檢查 malloc 失敗。
Node* insert(Node *head, int data) {
Node *newNode = malloc(sizeof(Node));
if (!newNode) return head;
newNode->data = data; newNode->next = NULL;
if (!head || data < head->data) {
newNode->next = head;
return newNode;
}
Node *cur = head;
while (cur->next && cur->next->data < data) cur = cur->next;
newNode->next = cur->next;
cur->next = newNode;
return head;
}