# This problem requires the implementation of a function to reverse the given one-way linked list, that is, the header is set to the end of the table, and the end of the table is set to the header.

2022-02-03 01:07:47

``#include <stdio.h>#include <stdlib.h>struct ListNode {    int data;    struct ListNode *next;};struct ListNode *createlist(); /* The referee realizes , Details don't show */struct ListNode *reverse( struct ListNode *head );void printlist( struct ListNode *head ){     struct ListNode *p = head;     while (p) {           printf("%d ", p->data);           p = p->next;     }     printf("\n");}int main(){    struct ListNode  *head;    head = createlist();    head = reverse(head);    printlist(head);    return 0;}/*  Your code will be embedded here  */struct ListNode* reverse(struct ListNode* head){    struct ListNode* p;    struct ListNode* q;    p = NULL;    q = head->next;    while (q != NULL)    {        head->next = p;        p = head;        head = q;        q = q->next;    }    return head;}``

Input 1 2 3 4 5 6 -1
Output 6
Is there anything wrong with the code

Refer to the answer 1：

while At the beginning of the cycle p=NULL,head->next=p=NULL 了 .
A linked list with leading nodes （ The first node data Some do not save data , The head node is just a fixed tag node ） The reverse ：

``// Flip struct ListNode* reverse(struct ListNode* head){    struct ListNode* p,*t,*k;    p = head->next;    t = p->next;    p->next = NULL;    while(t)    {        k = t->next;        head->next = t;        t->next = p;        p = t;        t = k;    }    return head;}``

Refer to the answer 2：

Refer to the answer 3：

Do you have a fixed head node or not , I can't see your createlist How to write

Refer to the answer 4：

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030107451787.html