class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* temp1;
ListNode* temp2;
ListNode* cur = dummyHead;
while (cur->next != NULL && cur->next->next != NULL)
{
temp1 = cur->next;
temp2 = cur->next->next->next;
cur->next = cur->next->next;
cur->next->next = temp1;
cur->next->next->next = temp2;
cur = cur->next->next;
}
return dummyHead->next;
}
};
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* fastNode = dummyHead;
ListNode* slowNode = dummyHead;
ListNode* temp;
n++;
while (n-- != 0 && fastNode != NULL) {
fastNode = fastNode->next;
}
while (fastNode != NULL) {
fastNode = fastNode->next;
slowNode = slowNode->next;
}
temp = slowNode->next;
slowNode->next = slowNode->next->next;
delete temp;
return dummyHead->next;
}
};
ListNode* curA = headA;
ListNode* curB = headB;
int lenA = 0, lenB = 0;
while (curA != NULL) {
lenA++;
curA = curA->next;
}
while (curB != NULL) {
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
if (lenB > lenA) {
swap (lenA, lenB);
swap (curA, curB);
}
int gap = lenA - lenB;
while (gap--) {
curA = curA->next;
}
while (curA != NULL) {
if (curA == curB) {
return curA;
}
curA = curA->next;
curB = curB->next;
}
return NULL;
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;
while (fast != NULL && fast->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
if (fast == slow)
{
ListNode* index1 = fast;
ListNode* index2 = head;
while (index1 != index2)
{
index1 = index1->next;
index2 = index2->next;
}
return index2;
}
}
return NULL;
}
};
slow = x + y;
fast = x + y + n(y + z);
2(x + y) = x + y + n(y + z);
x + y = n(y + z);
x = (n - 1)(y + z) + z;

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?