int main() { List L; creatList(&L); printList(L); // reverseList(L); reverseList2(L); // printList(L); }
int main(){ struct ListNode *list, *node1, *node2; node1 = (struct ListNode *)malloc(sizeof(struct ListNode)); node2 = (struct ListNode *)malloc(sizeof(struct ListNode)); node1->val=1; node1->next = node2; node2->val=2; node2->next=NULL; list=node1; print(list); // print(reverseList(list)); print(reverseList2(list)); return 0; }
// recursive version struct ListNode* reverseList2(struct ListNode* head) { struct ListNode *next; if(head == NULL) return NULL; next = head->next; if(next == NULL) return head; else{ head->next = NULL; struct ListNode *newhead = reverseList2(next); next->next = head; return newhead; } }