编辑代码

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int value;
    struct Node* next;
};

void initList (struct Node* head) {
    struct Node* node1 = (struct Node*)malloc(sizeof(struct Node));
    struct Node* node2 = (struct Node*)malloc(sizeof(struct Node));
    struct Node* node3 = (struct Node*)malloc(sizeof(struct Node));
    node1->value = 2;
    node1->next = head->next;
    head->next = node1;

    node2->value = 3;
    node2->next = node1->next;
    node1->next = node2;

    node3->value = 5;
    node3->next = node2->next;
    node2->next = node3;
}

void insertNode (struct Node* head, int num) {
    struct Node* temp;
    temp = head;
    if (temp->value < num) {
        while(temp->next != NULL) {
            if (temp->next->value >= num) {
                break;
            }

            temp = temp->next;
        }
    }    

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->value = num;
    newNode->next = temp->next;
    temp->next = newNode;
}

void reverseLinkList(struct Node* head) {
    struct Node* prev = NULL;
    struct Node* current = head->next;
    struct Node* next;
    while (current) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    head->next = prev; // 头结点指向新首结点
}

int main () {
    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    head -> value = 0;
    head -> next = NULL;

    initList(head);
    
    insertNode(head, 0);
    insertNode(head, 1);
    insertNode(head, 4);
    insertNode(head, 6);

    // 就地逆置
    reverseLinkList(head);

    struct Node* nodeTemp;
    nodeTemp = head;
    while (nodeTemp != NULL) {
        printf("链表数据:%d\n", nodeTemp -> value);
        nodeTemp = nodeTemp -> next;
    }

    return 0;
}