/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode** current=&head;
while(*current!=NULL&&(*current)->next!=NULL){
ListNode* tmp=(*current)->next;
(*current)->next=(*current)->next->next;
tmp->next=*current;
*current=tmp;
current=&(*current)->next->next;
}
return head;
}
};
Leetcode: Swap nodes in pairs
最新推荐文章于 2024-10-10 01:02:39 发布