/**
* 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 || head->next == NULL)
return head;
ListNode* p = head;
while(p)
{
if(p->next)
{
swap(p->val,p->next->val);
p = p->next->next;
}
else
{
break;
}
}
return head;
}
};
list swap pairs
最新推荐文章于 2022-10-13 15:35:41 发布