Leetcode 24

//需要注意的就是只用一个pre就能完成交换,一次AC,happy,已经忘记了第一次写链表时候的痛苦,所以说算法没难的,只不过练的欠火候。

/*
* * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* swapPairs(ListNode* head) { if(head == NULL)return NULL; ListNode *res = new ListNode(0); res->next = head; ListNode *pre = res; while(pre->next && pre->next->next){ ListNode *temp = pre->next->next->next; pre->next->next->next = pre->next; pre->next = pre->next->next; pre->next->next->next = temp; pre = pre->next->next; } return res->next; } };

猜你喜欢

转载自www.cnblogs.com/cunyusup/p/9663174.html