交换链表节点
struct ListNode {
int val;
struct ListNode *next;
ListNode(int val_) :val(val_) {}
};
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (head == nullptr || head->next == nullptr)
{
return head;
}
ListNode* pHead = new ListNode(0);
pHead->next = head;
ListNode* pNode = head;
int count = 0;
while (pNode != nullptr)
{
pNode = pNode->next;
count++;
}
pNode = head;
ListNode* pre = pHead;
for (int i = 0; i < (count / 2); ++i)
{
for (int j = 1; j < 2; ++j)
{
ListNode* next = pNode->next;
pNode->next = next->next;
next->next = pNode;
pre->next = next;
}
pre = pNode;
pNode = pNode->next;
}
return pHead->next;
}
};