抱歉啊 前天项目要结题了就没更了 现在补上~
24.两两交换链表中的节点
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int val) : val(val), next(NULL) {}
};
class Solution {
public:
ListNode* swapPair(ListNode* head) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* cur = dummyHead;
while (cur->next != nullptr && cur->next->next != nullptr) {
ListNode* tmp = cur->next; // 记录临时节点
ListNode* tmp1 = cur->next->next->next; // 记录临时节点
cur->next = cur->next->next; // 步骤一
cur->next->next = tmp; // 步骤二
cur->next->next->next = tmp1; // 步骤三
cur = cur->next->next; // cur移动两位,准备下一轮交换
}
return dummyHead->next;
}
};
// 主函数
int main() {
Solution solution;
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(3);
head->next->next->next = new ListNode(4);
ListNode* a = solution.swapPair(head);
while (a) {
cout << a->val << " ";
ListNode* temp = a;
a = a->next;
delete temp;
}
return 0;
}
19.删除链表的倒数第N个节点
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
进阶:你能尝试使用一趟扫描实现吗?
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int val) : val(val), next(NULL) {}
};
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* fastNode = dummyHead;
ListNode* slowNode = dummyHead;
while (n-- && fastNode != NULL) {
fastNode = fastNode->next;
}
fastNode = fastNode->next;
while (fastNode != NULL) {
fastNode = fastNode->next;
slowNode = slowNode->next;
}
ListNode* tmp = slowNode->next;
slowNode->next = slowNode->next->next;
delete tmp;
return dummyHead->next;
}
};
// 主函数
int main() {
Solution solution;
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(3);
head->next->next->next = new ListNode(4);
int target = 2;
ListNode* a = solution.removeNthFromEnd(head, target);
while (a) {
cout << a->val << " ";
ListNode* temp = a;
a = a->next;
delete temp;
}
return 0;
}
160.链表相交
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。
#include <iostream>
using namespace std;
// 定义链表节点结构体
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* curA = headA;
ListNode* curB = headB;
int lenA = 0, lenB = 0;
while (curA != NULL) { // 求链表A的长度
lenA++;
curA = curA->next;
}
while (curB != NULL) { // 求链表B的长度
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
swap (lenA, lenB);
swap (curA, curB);
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
while (gap--) {
curA = curA->next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != NULL) {
if (curA == curB) {
return curA;
}
curA = curA->next;
curB = curB->next;
}
return NULL;
}
};
// 主函数
int main() {
// 创建链表A:1->2->3->4->5
ListNode *headA = new ListNode(1);
headA->next = new ListNode(2);
headA->next->next = new ListNode(3);
headA->next->next->next = new ListNode(4);
headA->next->next->next->next = new ListNode(5);
// 创建链表B:6->7->8
ListNode *headB = new ListNode(6);
headB->next = new ListNode(7);
headB->next->next = new ListNode(8);
// 将链表B的末尾连接到链表A的第三个节点(即相交节点)
headB->next->next->next = headA->next->next;
Solution solution;
ListNode *intersectionNode = solution.getIntersectionNode(headA, headB);
if (intersectionNode)
cout << "Intersection node value: " << intersectionNode->val << endl;
else
cout << "No intersection node found." << endl;
return 0;
}
142.环形链表ii
题意: 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
为了表示给定链表中的环,使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
#include <iostream>
using namespace std;
// 定义链表节点结构体
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* detectCycle(ListNode* head) {
ListNode* fastNode = head;
ListNode* slowNode = head;
while (fastNode != NULL && fastNode->next != NULL) {
fastNode = fastNode->next->next;
slowNode = slowNode->next;
if (fastNode == slowNode) {
ListNode* index1 = fastNode;
ListNode* index2 = head;
while (index1 != index2)
{
index1 = index1->next;
index2 = index2->next;
return index2;
}
}
}
return NULL;
}
};
// 主函数
int main() {
ListNode* head = new ListNode(1);
ListNode* current = head;
for (int i = 2; i <= 9; ++i) {
current->next = new ListNode(i);
current = current->next;
}
// 构造环,使链表的尾节点指向第三个节点(即形成环)
current->next = head->next->next->next;
Solution solution;
ListNode* cycleStartNode = solution.detectCycle(head);
if (cycleStartNode)
cout << "Cycle starts at node with value: " << cycleStartNode->val << endl;
else
cout << "No cycle detected." << endl;
return 0;
}