LeetCode 24. Swap Nodes in Pairs
原题链接
简单
作者:
柒柒
,
2019-09-29 13:14:59
,
所有人可见
,
阅读 615
C++ 代码
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* headPtr = new ListNode(0);
headPtr->next = head;
ListNode* cur = headPtr;
ListNode* first = NULL;
ListNode* second = NULL;
while (cur->next != NULL) {
first = cur->next;
if (first == NULL)
break;
second = first->next;
if (second == NULL)
break;
cur->next = second;
first->next = second->next;
second->next = first;
cur = first;
}
return headPtr->next;
}
};