/**
* 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)
{
return head;
}
ListNode* first = head;
ListNode* second = head->next;
int temp = 0;
while (second)
{
int temp = first->val;
first->val = second->val;
second->val = temp;
first = first->next->next;
if (first)
{
second = first->next;
}
else
{
second = NULL;
}
}
return head;
}
};
LeetCode两两交换链表的节点
最新推荐文章于 2024-09-03 18:29:17 发布