24. 两两交换链表中的节点
题目链接:. - 力扣(LeetCode)
这个题目也是涉及到相邻两点进行交换的算法。在你交换两个节点时,你需要保存前面的节点和后面的节点。所以其实想要交换两个点的过程中,总共涉及到了四个数据点。
需要按照图示的顺序进行操作。实现代码如下:
/**
* 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) {
ListNode* cur = new ListNode(0);
cur->next = head;
if (head == nullptr || head->next == nullptr)
return head;
ListNode* pre = cur;
ListNode* beh = cur->next;
while (beh != nullptr && beh->next != nullptr) {
ListNode* temp1 = beh->next;
// ListNode* temp2=nullptr;
ListNode* temp2 = beh->next->next;
pre->next = temp1;
temp1->next = beh;
beh->next = temp2;
pre = beh;
beh = pre->next;
}
return cur->next;
}
};
19.删除链表的倒数第N个节点
题目链接:. - 力扣(LeetCode)
需要在O(n)的时间复杂度下找到链表的倒数第n个节点。这个trick算法大二数据结构课上有接触过。其实本质上还是用到了双指针算法,即设置两个双指针,这两个指针的距离是n,然后一起移动指针,当后面的指针移动到了最后一个链表节点时,前面的指针也就知道了待删除节点的前一个节点,就可以直接进行删除操作。
实现代码如下:
/**
* 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* removeNthFromEnd(ListNode* head, int n) {
ListNode* cur=new ListNode(0);
cur->next=head;
ListNode* pre=cur;
ListNode* beh=cur;
while(n--)
{
if(beh->next!=nullptr) beh=beh->next;
}
while(beh->next!=nullptr&&pre->next!=nullptr)
{
pre=pre->next;
beh=beh->next;
}
ListNode* temp=pre->next;
pre->next=pre->next->next;
delete temp;
return cur->next;
}
};
面试题 02.07. 链表相交
这个题的难点是找到两个链表上的相同的一段子链表。也就是找到第一个地址相同的指针。自己做的时候并没有想到合理的解法,看了讲解文章之后才悟了。
把两个链表进行尾对齐,然后从较短链表的头节点开始一期遍历,依次进行地址比较,从而找到相同地址的相交节点。
实现代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
ListNode* cur1 = new ListNode(0);
ListNode* cur2 = new ListNode(0);
cur1->next = headA;
cur2->next = headB;
ListNode* point1 = cur1;
ListNode* point2 = cur2;
int len1 = 0, len2 = 0;
while (point1->next != NULL) {
point1 = point1->next;
len1++;
} // 获取链表1的长度
while (point2->next != NULL) {
point2 = point2->next;
len2++;
} // 获取链表2的长度
if (len1 >= len2) {
ListNode* point1 = cur1;
ListNode* point2 = cur2;
int len=len1-len2+1;
while (len--) {
point1 = point1->next;
}
point2 = point2->next;
while (point1 != point2 && point1 != NULL && point2 != NULL) {
point1 = point1->next;
point2 = point2->next;
}
if (point1 == point2)
return point1;
else
return NULL;
} else {
ListNode* point1 = cur1;
ListNode* point2 = cur2;
int len=len2-len1+1;
while (len--) {
point2 = point2->next;
}
point1 = point1->next;
while (point1 != point2 && point1 != NULL) {
point1 = point1->next;
point2 = point2->next;
}
if (point1 == point2)
return point1;
else
return NULL;
}
}
};
142.环形链表II
这个题就很难了。涉及到了两个知识点,一个是对链表中的环路进行判断,第二点是找到链表中的环路。
首先来看看怎么判断链表中是否存在环路,也是看了文章讲解后学会的。主要思路是设置两个指针,一个指针每次往前走两个节点,另一个指针每次往前走一个节点。他们如果相遇到了某个节点,说明这个链表存在环路,反之则不存在环路。
判断这是一个环路之后,需要找到这个环形链表的入口节点。这里需要一定的数学计算。
真的是很妙的思路,但让我自己手推确实有点想不到。只能说需要更多的刷题锻炼思维。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
ListNode* cur1 = new ListNode(0);
ListNode* cur2 = new ListNode(0);
cur1->next = headA;
cur2->next = headB;
ListNode* point1 = cur1;
ListNode* point2 = cur2;
int len1 = 0, len2 = 0;
while (point1->next != NULL) {
point1 = point1->next;
len1++;
} // 获取链表1的长度
while (point2->next != NULL) {
point2 = point2->next;
len2++;
} // 获取链表2的长度
if (len1 >= len2) {
ListNode* point1 = cur1;
ListNode* point2 = cur2;
int len=len1-len2+1;
while (len--) {
point1 = point1->next;
}
point2 = point2->next;
while (point1 != point2 && point1 != NULL && point2 != NULL) {
point1 = point1->next;
point2 = point2->next;
}
if (point1 == point2)
return point1;
else
return NULL;
} else {
ListNode* point1 = cur1;
ListNode* point2 = cur2;
int len=len2-len1+1;
while (len--) {
point2 = point2->next;
}
point1 = point1->next;
while (point1 != point2 && point1 != NULL) {
point1 = point1->next;
point2 = point2->next;
}
if (point1 == point2)
return point1;
else
return NULL;
}
}
};