Title: Enter the head node of a linked list, reverse the list, and return the head node of the inverted list. The linked list node is defined as follows:
struct ListNode
{
int M_nkey;
listnode* M_pnext;
};
C Language Implementation
/*file:rlink.cauthor:date:2015/4/4platform:windows7 x86_64version:1.0function: Reverse a list */#include <stdio.h> #include <stdlib.h>typedef struct _linknode{int m_nkey;struct _linknode* m_npnext;} Linknode, *plinknode;/* creates a linked list with 0 endings */plinknode create () {Plinknode phead = NULL, Pcur, Pnext;int ndata;if (scanf ("%d", &am P;ndata), ndata) {phead = (plinknode) malloc (sizeof (linknode));p cur = pnext = Phead;phead->m_nkey = Ndata;pnext->m_ Npnext = NULL;} Else{return NULL;} while (scanf ("%d", &ndata), ndata) {Pnext = (plinknode) malloc (sizeof (Linknode));p Next->m_nkey = ndata;pcur-> M_npnext = Pnext;pcur = Pnext;pnext->m_npnext = NULL;} return phead;} void ShowLink (Plinknode phead) {while (Phead) {printf ("%d", phead->m_nkey);p head = Phead->m_npnext;} printf ("\ n");} Plinknode Reverselink (Plinknode phead) {plinknode pcur, prhead;p cur = phead->m_npnext;if (!phead) {return NULL;} Prhead = Phead;while (pcur) {phead->m_npnext = pcur->m_npnext; A->b->c-&gT;d->null Phead = Prhead = A Pcur->m_npnext = prhead; Pcur->m_npnext into a new head node Prhead = Pcur;pcur = phead->m_npnext; Pcur is always Phead->m_npnext}return prhead;} int main (int argc, char *argv[]) {Plinknode Phead = create (); ShowLink (phead);p head = Reverselink (Phead); ShowLink (Phead); return 0;}
Title: Enter the head node of a linked list, reverse the list, and return the head node of the inverted list.