/**
* 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)
{
ListNode* re;
ListNode* pre=new ListNode(1);
pre->next=head;
if(!head)
{
return NULL;
}
else
{
if(head->next!=NULL)
re=head->next;
else
return head;
}
while(head!=NULL&&head->next!=NULL)
{
ListNode* second=head->next;
head->next=second->next;
pre->next=second;
second->next=head;
pre=head;
head=head->next;
}
return re;
}
};
Leetcode——24. 两两交换链表中的节点
最新推荐文章于 2024-06-15 23:15:30 发布