【问题标题】:How to skip nodes of starting in linked list如何跳过链表中的起始节点
【发布时间】:2014-04-09 15:20:44
【问题描述】:

我正在尝试处理指针。 我的情况是我想在第二个节点(从第三个节点)之后遍历链表。

我遍历链表的基本思路是:

while(temp!=NULL)
{
 temp=temp->next;
} 

所以我想要的是这个链表必须从第三个位置开始。谁能帮我为此准备逻辑? 我对使用任何内置函数不感兴趣。 我将在其他应用程序中使用此逻辑。

【问题讨论】:

    标签: c algorithm data-structures linked-list singly-linked-list


    【解决方案1】:
    Node *nth(Node *head, int nth){//nth : 0 origin
        while(nth && head){
            --nth;
            head = head->next;
        }
        return head;
    }
    ...
    Node *third = nth(head, 2);
    

    【讨论】:

      【解决方案2】:

      如果您的列表是temp,那么temp 也是第一个元素。

      那么temp->next 将是第二个元素,temp->next->next 将是第三个元素。

      所以如果你写

      temp = temp->next->next;
      

      temp 将指向第三个元素,您可以从那里开始遍历。

      【讨论】:

        猜你喜欢
        • 2012-06-08
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        相关资源
        最近更新 更多