# 12568 - Reverse Linked List ver 2 ## 題解: **Reference:** https://reurl.cc/M7x6vk ## Code: ```cpp #include "function.h" #include <stdio.h> #include <stdlib.h> void Push(Node** ptr_head, int x){ // create new node Node* new_node = (Node*)malloc(sizeof(Node)); new_node->data = x; // push front new_node->next = *ptr_head; *ptr_head = new_node; } void Pop(Node** ptr_head){ // empty if(*ptr_head == NULL) return; // pop front Node* cur = *ptr_head; *ptr_head = (*ptr_head)->next; free(cur); cur = NULL; } void Reverse_List(Node** ptr_head){ // empty or only one node if(*ptr_head == NULL || (*ptr_head)->next == NULL) return; // reverse Node *previous = NULL, *cur = *ptr_head, *preceding = (*ptr_head)->next; while(preceding != NULL){ cur->next = previous; previous = cur; cur = preceding; preceding = preceding->next; } // last node is new head cur->next = previous; *ptr_head = cur; } ``` ###### tags: `NTHUOJ`