标签:sizeof while app star swa end 交换 list 链表
一、非递归
1 ListNode* swapPairs(ListNode* head) { 2 ListNode* pre = (ListNode*)malloc(sizeof(ListNode)); 3 pre->next = head; 4 ListNode* temp = pre; 5 6 while (temp->next&&temp->next->next) 7 { 8 ListNode* start = temp->next; 9 ListNode* end = temp->next->next; 10 11 temp->next = end; 12 start->next = end->next; 13 end->next = start; 14 15 temp = start; 16 } 17 return pre->next; 18 }
二、递归
1 ListNode* swapPairs(ListNode* head) { 2 if (head == NULL || head->next == NULL) 3 return head; 4 ListNode* next = head->next; 5 head->next = swapPairs(next->next); 6 next->next = head; 7 return next; 8 }
标签:sizeof while app star swa end 交换 list 链表
原文地址:https://www.cnblogs.com/zouma/p/11505008.html