Huawei machine questions -- reverse order of single-chain table
[Problem]
Reverse Sequence of a single-chain table
[Code]
# Include
# Include
# Include
Typedef struct ListNode {int value; struct ListNode * next;} ListNode; typedef struct ListNode * List; List reverseList (List head) // List reverse {ListNode * rear, * curr, * front; rear = curr = head; while (curr! = NULL) {front = curr-> next; curr-> next = rear; rear = curr; curr = front;} head-> next = NULL; head = rear; return head;} List createdList (int a [], int len) // create a List {int I = 1; ListNode * head, * curr, * rear; curr = (ListNode *) malloc (sizeof (ListNode); curr-> value = a [0]; curr-> next = NULL; head = rear = curr; while (I <len) {curr = (ListNode *) malloc (sizeof (ListNode); curr-> value = a [I ++]; curr-> next = NULL; Rear-> next = curr; rear = curr;} return head;} void printList (List head) // print the List {ListNode * curr; curr = head; while (curr! = NULL) {printf ("% d", curr-> value); curr = curr-> next ;}} int main (void) {int a [5] = {1, 2, 3, 4, 5}; List head; head = createdList (a, 5); head = reverseList (head ); printList (head); printf ("\ n"); return 0 ;}