链表逆序 头插法
#include <stdio.h> #include <stdlib.h> typedef struct Node{ int content; struct Node* next; }LNode; init(LNode** head){ *head = (LNode*)malloc(sizeof(LNode)); (*head)->content = 0; (*head)->next = NULL; } insert(LNode* head, int num){ LNode* newNode = (LNode*)malloc(sizeof(LNode)); newNode->content = num; newNode->next = head->next; head->next = newNode; } printL(LNode* head){ head = head->next; while (head != NULL){ printf("%d ", head->content); head = head->next; } } zhuanZhi(LNode* head){ LNode* p = head->next; LNode* q = p->next;//获得第二个 p->next = NULL;//初始化第一个 while (q != NULL){ head->next = q;//先插进去 head->next->next = p;//逆序 p = head->next;//迭代p q = q->next;//迭代q } } main(){ LNode* head; init(&head); insert(head, 1); insert(head, 2); insert(head, 3); insert(head, 4); insert(head, 5); insert(head, 6); insert(head, 7); insert(head, 8); insert(head, 9); printL(head); zhuanZhi(head); printL(head); }