網(wǎng)站工作溝通及建設(shè)seo培訓(xùn)教程
?你存在,我深深的腦海里~
題目:
示例:
思路:
這個(gè)題有點(diǎn)類(lèi)似于反轉(zhuǎn)一個(gè)單鏈表,不同的地方在于這個(gè)題不全反轉(zhuǎn),所以我們不同的地方在于此題多用了一個(gè)prve指針保存n1的前一個(gè)節(jié)點(diǎn),以及頭的改變,用newhead保存一個(gè)新的頭,其他都大同小異,參考:反轉(zhuǎn)一個(gè)單鏈表
代碼:
struct ListNode* swapPairs(struct ListNode* head)
{if (head == NULL)return NULL;struct ListNode* newhead = head;struct ListNode* n1 = head;struct ListNode* n2 = NULL;struct ListNode* n3 = NULL;struct ListNode* prve = NULL;while (n1 && n1->next){n2 = n1->next;n3 = n2->next;if (n1 == head){n1->next = n2->next;n2->next = n1;newhead = n2;}else{n1->next = n2->next;n2->next = n1;prve->next = n2;}prve = n1;n1 = n3;}return newhead;
}
個(gè)人主頁(yè):Lei寶啊
愿所有美好如期而遇