Topics
Ideas
Flip one-way linked list, here the topic requires recursive and non-recursive implementation, the specific ideas see code.
Code
a) non-recursive
struct ListNode* reverseList(struct ListNode* head) { structNULL; struct ListNode * OriPresent = head; whileNULL) { struct ListNode * Present = (struct ListNode*)malloc(sizeof(struct ListNode)); Present->val = OriPresent->val; Present->next = Before; Before = Present; OriPresent = OriPresent->next; } return Before;}
b) recursion
struct listnode * Reverse (struct ListNode * before, struct ListNode * oripresent) {if (oripresent = = null ) return before; struct ListNode * Present = (struct listnode*) malloc (sizeof (struct listnode)); Present->next = before; Present->val = oripresent->val; return reverse (Present, oripresent->next);} struct listnode* reverselist (struct listnode* head) {return< /span> Reverse (null , head);}
Leetcode OJ Reverse Linked List