You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

仅使用头指针直接遍历链表的可行性疑问及代码验证

仅使用头指针直接遍历链表的可行性疑问及代码验证

Hey there! No worries at all—everyone starts somewhere with DSA, and this is actually a fantastic question to get clear on early. Let’s break this down step by step.

First off, great observation: both of your print function implementations will work perfectly and produce the exact same output for your linked list. The key difference between them boils down to whether you need to preserve the original pointer's position, but let's dig into the details:

1. Using a temporary pointer (ptr)

Your first version creates a temporary pointer ptr that starts pointing to the same node as head, then uses ptr to traverse the list. The big advantage here is that after the traversal, the original head pointer (the one passed into the function) still points to the first node of the list. This matters if you need to use that original pointer for more operations inside the same function (like resetting to the start to do another traversal, or modifying the head node itself).

Here's your code for reference:

void printNodes(node* head) {
    if(head == NULL)
        printf("Linked list is empty");
    node *ptr = head;
    while(ptr != NULL) {
        printf("%d ", ptr->data);
        ptr = ptr->link;
    }
}

2. Directly using the head parameter

Your second version skips the temporary pointer and uses the head parameter directly for traversal. You might be worried this breaks something—but in C, function parameters are passed by value. That means the head inside your printNodes function is just a copy of the original head pointer from main. When you do head = head->link, you're only modifying this copy, not the original pointer in main.

So for a print function (where you don't need to keep the original pointer's position for later use in the same function), this approach is totally safe and valid. It even makes the code a bit more concise by eliminating the extra temporary variable.

Here's that code again:

void printNodes(node *head) {
    if(head == NULL)
        printf("Linked list is empty");
    while(head != NULL){
        printf("%d ", head->data);
        head = head->link;
    }
}

Quick summary

  • For your specific printNodes use case, both methods are equally correct and will work as expected.
  • Use the temporary pointer approach if you need to retain the original pointer's position for additional operations within the same function.
  • Using the parameter directly is fine (and even cleaner) when you don't need to preserve that original position, since you're only modifying a copy of the pointer.

Your main function code is solid too—it correctly builds a 3-node linked list and calls the print function properly. Keep up the good work!

备注:内容来源于stack exchange,提问作者itzmartin_20

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 10:13:15