目录
题目描述:
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
示例:
给定1->2->3->4
, 你应该返回2->1->4->3
.
说明:
- 你的算法只能使用常数的额外空间。
- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
解题思路:
定三个指针:pre指向前面节点, cur指向当前节点, post指向后面节点。指向如下:
先将pre->next指向后面的节点post , 再交换cur和post位置:cur->next = post->next; post->next = cur;
交换后变成:
移动到下一组,即3,4的一组,pre移动到 1: pre = pre->next->next; 这样pre指向1,判断后面是否有两个节点,即pre-next和pre->next->next。
C++代码:
/**
* 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 * dummy = new ListNode(0); // 生成哑结点,并指向真正的头结点head
dummy->next = head;
ListNode* pre = dummy;
while(pre->next && pre->next->next){
ListNode* cur = pre->next;
ListNode* post = pre->next->next; // cur和post组成当前需要交换的一组
pre->next = post; // 指向后面的节点,使其成为前面的节点
cur->next = post->next; // 交换cur和post两个节点的位置
post->next = cur;
pre = pre->next->next; // 下一组
}
return dummy->next;
}
};
c++代码 (思路2)
四个指针,slow,p,q,fast,其中slow和fast指向要交换两个节点p和q的前一个节点和后一个节点,
交换p和q,只要q不为空,则一直交换,否则停止。
执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
内存消耗:7.3 MB, 在所有 C++ 提交中击败了66.28%的用户
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
// 特殊情况,空或者只有一个节点
if (head == nullptr || head->next == nullptr) return head;
// 4个指针slow,p,q,fast. slow指向要交换两个节点p和q的前一个节点和后一个节点
ListNode* p_head = new ListNode(0);
p_head->next = head;
ListNode* slow = p_head;
ListNode* p = slow->next;
ListNode* q = p->next; // 前面排除了一个节点的情况,所以q肯定不为空
ListNode* fast = q->next;
while (q) // q不空,p肯定不空,只要这两个不空,则需要交换
{
// 交换
p->next = q->next;
q->next = p;
slow->next = q; // 由于更换了pq,所以slow下一个节点换成了q
// 更新slow,p,q,fast
slow = p; // 前面更换了pq
if (slow->next == nullptr || slow->next->next == nullptr) break; // 只要p或者q为空,则停止交换
else
{
p = slow->next; // p可能为空
q = p->next;
fast = q->next;
}
}
return p_head->next;
}
};
python代码:
执行用时:16 ms, 在所有 Python 提交中击败了72.97%的用户
内存消耗:13.2 MB, 在所有 Python 提交中击败了35.34%的用户
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
# 特殊情况
if head is None or head.next is None: return head
# slow,p,q,fast
p_head = ListNode(0)
p_head.next = head
slow = p_head
p = slow.next
q = p.next
fast = q.next
while q is not None: # 只要q不空,则p和q都不空
# 交换
p.next = q.next
q.next = p
slow.next = q
# 更新slow,p,q,fast
slow = p
if slow.next is None or slow.next.next is None: break # p或者q为空,则不更新了,直接跳出循环
else:
p = slow.next
q = p.next
fast = q.next
return p_head.next