c语言怎么把链表的第一个元素移动到最后一个?

how to move first element to last of a linked list in c language?

我正在尝试获取链表的第一个元素并将其移动到最后一个位置。我用这段代码做到了:

            pend=pstart;
            while(pend->next != NULL) // go to the last element
            {
                pend = pend->next;
            }
            pend->next=pstart;
            pend=pstart;
            pstart=pstart->next;
            pend->next=NULL;

但似乎我可能遗漏了一些东西,因为我没有得到我想要的结果。所以,我的问题是:这段代码是否正确?如果没有请帮我解决它。谢谢。

        pend=pstart;
        while(pend->next != NULL) // go to the last element
        {
            pend = pend->next;
        }
        pend->next=pstart;
        pstart=pstart->next;
        pend->next->next= null;

您的代码是正确的。你在别的地方搞砸了。

解决此类问题的最好方法是使用框(节点)和箭头(链接)绘制链表。