24. 两两交换链表中的节点,交换时注意细节,链表通过虚拟节点进行一些操作好理解,代码如下:
#include <iostream>
#include <vector>
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode *swapPairs(ListNode *head)
{
ListNode *dummy = new ListNode(0);
dummy->next = head;
ListNode *current = dummy;
while (current->next != nullptr && current->next->next != nullptr)
{
ListNode *first = current->next;
ListNode *second = current->next->next;
// 交换操作
first->next = second->next;
second->next = first;
current->next = second;
// 移动到下一对节点
current = first;
}
ListNode *newHead = dummy->next;
delete dummy; // 不要忘记删除哨兵节点
return newHead;
}
// 用于创建链表的辅助函数
ListNode *createList(const std::vector<int> &values)
{
ListNode *dummy = new ListNode(0);
ListNode *tail = dummy;
for (int val : values)
{
tail->next = new ListNode(val);
tail = tail->next;
}
return dummy->next;
}
// 用于打印链表的辅助函数
void printList(ListNode *head)
{
while (head != nullptr)
{
std::cout << head->val << " ";
head = head->next;
}
std::cout << std::endl;
}
int main()
{
ListNode *list = createList({1, 2, 3, 4});
std::cout << "Original list: ";
printList(list);
ListNode *swappedList = swapPairs(list);
std::cout << "Swapped list: ";
printList(swappedList);
return 0;
}
19.删除链表的倒数第N个节点,双指针,但是注意fast首先走n + 1步
#include <iostream>
#include <vector>
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode *removeNthFromEnd(ListNode *head, int n)
{
ListNode *dummy = new ListNode(0);
dummy->next = head;
ListNode *first = dummy;
ListNode *second = dummy;
// 移动 first 指针,使其与 second 指针之间相隔 n+1 个节点
for (int i = 0; i <= n; i++)
{
first = first->next;
}
// 同时移动 first 和 second 指针
while (first != nullptr)
{
first = first->next;
second = second->next;
}
// second 指针现在指向要删除节点的前一个节点
ListNode *toDelete = second->next;
second->next = second->next->next;
delete toDelete; // 释放内存
ListNode *newHead = dummy->next;
delete dummy; // 释放哨兵节点
return newHead;
}
// 用于创建链表的辅助函数
ListNode *createList(const std::vector<int> &values)
{
ListNode *dummy = new ListNode(0);
ListNode *tail = dummy;
for (int val : values)
{
tail->next = new ListNode(val);
tail = tail->next;
}
return dummy->next;
}
// 用于打印链表的辅助函数
void printList(ListNode *head)
{
while (head != nullptr)
{
std::cout << head->val << " ";
head = head->next;
}
std::cout << std::endl;
}
int main()
{
ListNode *list = createList({1, 2, 3, 4, 5});
std::cout << "Original list: ";
printList(list);
ListNode *updatedList = removeNthFromEnd(list, 2);
std::cout << "Updated list: ";
printList(updatedList);
return 0;
}
面试题 02.07. 链表相交,单链表有顺序,所以交点只可能在后面出现,所以用双指针先走到两者长度一致的地方开始遍历,并且对比即可
#include <iostream>
#include <vector>
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB)
{
ListNode *pA = headA;
ListNode *pB = headB;
if (headA == nullptr || headB == nullptr)
return nullptr;
while (pA != pB)
{
// 当一个指针走到链表末尾时,转向另一个链表的头部
pA = pA == nullptr ? headB : pA->next;
pB = pB == nullptr ? headA : pB->next;
}
// 返回交点,或者 nullptr
return pA;
}
// 用于创建链表的辅助函数(此处只是示例,实际使用中不需要这个函数)
ListNode *createList(const std::vector<int> &values)
{
ListNode *dummy = new ListNode(0);
ListNode *tail = dummy;
for (int val : values)
{
tail->next = new ListNode(val);
tail = tail->next;
}
return dummy->next;
}
int main()
{
// 示例链表构建(在实际应用中,需要确保链表相交)
ListNode *listA = createList({4, 1, 8, 4, 5});
ListNode *listB = createList({5, 6, 1, 8, 4, 5});
// 假设 8 是相交的起始节点
listA->next->next = listB->next->next->next;
ListNode *intersectionNode = getIntersectionNode(listA, listB);
if (intersectionNode != nullptr)
{
std::cout << "Intersection at node with value: " << intersectionNode->val << std::endl;
}
else
{
std::cout << "No intersection." << std::endl;
}
return 0;
}
142.环形链表II
#include <iostream>
#include <vector>
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode *detectCycle(ListNode *head)
{
if (head == nullptr || head->next == nullptr)
return nullptr;
ListNode *slow = head;
ListNode *fast = head;
ListNode *entry = head;
while (fast != nullptr && fast->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
{ // 两指针相遇,存在环
while (entry != slow)
{
entry = entry->next;
slow = slow->next;
}
return entry; // 环的入口
}
}
return nullptr; // 没有环
}
// 用于创建链表和环的辅助函数(此处只是示例,实际使用中需要根据需要调整)
ListNode *createListWithCycle(const std::vector<int> &values, int pos)
{
ListNode *dummy = new ListNode(0);
ListNode *tail = dummy;
ListNode *cycleEntry = nullptr;
for (int i = 0; i < values.size(); i++)
{
tail->next = new ListNode(values[i]);
tail = tail->next;
if (i == pos)
{
cycleEntry = tail;
}
}
if (cycleEntry != nullptr)
{
tail->next = cycleEntry; // 创建环
}
return dummy->next;
}
int main()
{
// 创建带环的链表,假设环的入口在索引 2 的位置
ListNode *list = createListWithCycle({3, 2, 0, -4}, 1);
ListNode *cycleNode = detectCycle(list);
if (cycleNode != nullptr)
{
std::cout << "Cycle detected at node with value: " << cycleNode->val << std::endl;
}
else
{
std::cout << "No cycle detected." << std::endl;
}
return 0;
}