目录
1.T24:两两交换链表中的节点
T:给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)
S:注意不是冒泡排序那样挨个挨个的交换,别误解!
建议使用虚拟头结点,这样方便很多,不然每次针对头结点(没有前一个指针指向头结点),还要单独处理。
接下来就是交换相邻两个元素了,此时一定要画图,不画图,操作多个指针很容易乱,而且要操作的先后顺序
初始时,cur指向虚拟头结点,然后三步走,代码如下:
代码实现:
C++:
ListNode* swapPairs(ListNode* head) {
ListNode* dummyHead = new ListNode(-1, head);
ListNode* cur = dummyHead;
while (cur->next != nullptr && cur->next->next != nullptr) {
ListNode* temp0 = cur->next;
ListNode* temp1 = cur->next->next->next;//cur->next->next->next可以为空指针
cur->next = cur->next->next;
cur->next->next = temp0;
cur->next->next->next = temp1;
cur = cur->next->next;//相当于下一个dummyHead
}
return dummyHead->next;
}
- 时间复杂度:O(n)
- 空间复杂度:O(1)
Java:
public ListNode swapPairs(ListNode head) {
ListNode dummyHead = new ListNode(-1, head);
ListNode cur = dummyHead;
// while (cur != null && cur.next.next != null) {//NullPointException
while (cur.next != null && cur.next.next != null) {//搞懂为什么是判断这俩
ListNode temp0 = cur.next;
ListNode temp1 = cur.next.next.next;
cur.next = cur.next.next;//Step1
cur.next.next = temp0;//Step2
cur.next.next.next = temp1;//Step3,易漏
cur = cur.next.next;//易漏
}
return dummyHead.next;//理解
}
2.T19:删除链表的倒数第N个节点
T:给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
S:
-
定义fast指针和slow指针,初始值为虚拟头结点;
-
fast首先走n + 1步 ,为什么是n+1呢?因为只有这样同时移动的时候slow才能指向删除节点的上一个节点(方便做删除操作);
-
fast和slow同时移动,直到fast指向末尾;
-
删除slow指向的下一个节点。
代码实现:
C++:
ListNode* removeNthFromEnd(ListNode* head, int n) {
// if (head == nullptr) return nullptr;
ListNode* dummyHead = new ListNode(-1, head);
ListNode* slow = dummyHead;
ListNode* fast = dummyHead;
while (n--) {
fast = fast->next;
}
fast = fast->next;
while (fast != nullptr) {
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
return dummyHead->next;
}
Java:
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyHead = new ListNode(-1, head);
ListNode slow = dummyHead, fast = dummyHead;
while (n-- >= 0) {
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
// slow.next = fast;
slow.next = slow.next.next;
return dummyHead.next;
}
3.T160:链表相交(面试题 02.07. 链表相交)
T:给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。
图示两个链表在节点 c1 开始相交:
题目数据 保证 整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构 。
S:
- 须知:如果两个链表相交,相交节点的值不能为0;如果无交点,则将交点值记为0。
简单来说,就是求两个链表交点节点的指针。 要注意,交点不是数值相等,而是指针相等。
总的来说,就是分别求出两条链表的长度后,将起始指针移到同一起跑线,然后开始逐一比较
代码实现:
C++:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headB == nullptr || headA == nullptr) return nullptr;
int lenA = 0, lenB = 0;
ListNode* curA = headA;
ListNode* curB = headB;
while (curA != nullptr) {
curA = curA->next;
++lenA;
}
while (curB != nullptr) {
curB = curB->next;
++lenB;
}
//重置(易漏)
curA = headA;
curB = headB;
if (lenB > lenA) {
lenB = lenB ^ lenA;
lenA = lenB ^ lenA;
lenB = lenB ^ lenA;
// curB = curB ^ curA;
// curA = curB ^ curA;
// curB = curB ^ curA;//error: invalid operands ti binary expression
swap(curB, curA);
}
int gap = lenA - lenB;
while (gap--) {
curA = curA->next;
}
// while (curA->next != nullptr) {
while (curA != nullptr) {
if (curA == curB) return curA;
curA = curA->next;
curB = curB->next;
}
return nullptr;
}
Java:
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) return null;
ListNode curA = headA, curB = headB;
int lenA = 0, lenB = 0;
while (curA != null) {
curA = curA.next;
++lenA;
}
while (curB != null) {
curB = curB.next;
++lenB;
}
curA = headA;
curB = headB;
if (lenA < lenB) {
lenB = lenB ^ lenA;
lenA = lenB ^ lenA;
lenB = lenB ^ lenA;
// curB = curB ^ curA;//error: bad operand types for binary operator '^'
// curA = curB ^ curA;
// curB = curB ^ curA;
ListNode temp = curA;
curA = curB;
curB = temp;
}
int gap = lenA - lenB;
while (gap-- > 0) {
curA = curA.next;
}
while (curA != null) {
if (curA == curB) return curA;
curA = curA.next;
curB = curB.next;
}
return null;
}
尚存疑惑:引用类型变量怎么在不使用临时变量的情况下实现变量交换?
4.T142:环形链表 II
T:给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。
S:这道题目,不仅考察对链表的操作,而且还需要一些数学运算。
- 主要考察两知识点:
- 判断链表是否有环
- 如果有环,如何找到这个环的入口
4.1.判断链表是否有环
可以使用快慢指针法,分别定义 fast 和 slow 指针,从头结点出发,fast指针每次移动两个节点,slow指针每次移动一个节点,如果 fast 和 slow指针在途中相遇 ,说明这个链表有环。
为什么fast 走两个节点,slow走一个节点,有环的话,一定会在环内相遇呢,而不是永远的错开呢
首先第一点:fast指针一定先进入环中,如果fast指针和slow指针相遇的话,一定是在环中相遇,这是毋庸置疑的。
为什么fast指针和slow指针一定会相遇呢?
这是因为fast是走两步,slow是走一步,其实相对于slow来说,fast是一个节点一个节点的靠近slow的,所以fast一定可以和slow重合。
4.2.如果有环,如何找到这个环的入口
此时已经可以判断链表是否有环了,那么接下来要找这个环的入口了。
假设从头结点到环形入口节点 的节点数为x。 环形入口节点到 fast指针与slow指针相遇节点 节点数为y。 从相遇节点 再到环形入口节点节点数为 z。
那么相遇时: slow指针走过的节点数为: x + y, fast指针走过的节点数:x + y + n (y + z),n为fast指针在环内走了n圈才遇到slow指针, (y+z)为 一圈内节点的个数A。
因为fast指针是一步走两个节点,slow指针一步走一个节点, 所以 fast指针走过的节点数 = slow指针走过的节点数 * 2:(x + y) * 2 = x + y + n (y + z)
两边消掉一个(x+y): x + y = n (y + z)
因为要找环形的入口,那么要求的是x,因为x表示 头结点到 环形入口节点的的距离。
所以要求x ,将x单独放在左面:x = n (y + z) - y ,
再从n(y+z)中提出一个 (y+z)来,整理公式之后为如下公式:x = (n - 1) (y + z) + z 注意这里n一定是大于等于1的,因为 fast指针至少要多走一圈才能相遇slow指针。
这个公式说明什么呢?
先拿n为1的情况来举例,意味着fast指针在环形里转了一圈之后,就遇到了 slow指针了。
当 n为1的时候,公式就化解为 x = z,
这就意味着,从头结点出发一个指针,从相遇节点 也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是 环形入口的节点。
那么 n如果大于1是什么情况呢,就是fast指针在环形转n圈之后才遇到 slow指针。
其实这种情况和n为1的时候 效果是一样的,一样可以通过这个方法找到 环形的入口节点,只不过,index1 指针在环里 多转了(n-1)圈,然后再遇到index2,相遇点依然是环形的入口节点。
4.3.代码实现:
C++:
ListNode *detectCycle(ListNode *head) {
if (head == nullptr) return nullptr;
ListNode* slow = head;
ListNode* fast = head;
// while (slow != fast) {
while (fast != nullptr && fast->next != nullptr) {
fast = fast->next->next;
slow = slow->next;
// 快慢指针相遇,此时从head 和 相遇点,同时查找直至相遇
if (slow == fast) {
ListNode* index1 = fast;
ListNode* index2 = head;//别本能写了slow
while (index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return nullptr;
}
Java:
public ListNode detectCycle(ListNode head) {
if (head == null) return null;
ListNode slow = head, 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;
}
至于Carl哥所说的,在推理过程中,大家可能有一个疑问就是:为什么第一次在环中相遇,slow的 步数 是 x+y 而不是 x + 若干环的长度 + y 呢?
不好意思没有疑惑这个,一想到就觉得怎么可能呢哈哈😂😂