代码随想录算法训练营第四天 | 24. 两两交换链表中的节点、 19. 删除链表的倒数第 N 个结点 、面试题 02.07. 链表相交、142. 环形链表 II
目录
- [24. 两两交换链表中的节点](https://leetcode.cn/problems/swap-nodes-in-pairs/)
- [19. 删除链表的倒数第 N 个结点](https://leetcode.cn/problems/remove-nth-node-from-end-of-list/)
- [面试题 02.07. 链表相交](https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/)
- [142. 环形链表 II](https://leetcode.cn/problems/linked-list-cycle-ii/)
24. 两两交换链表中的节点
思路
个人问题及总结
- 因为一直想尝试以反转链表的方式来解决(pre,cur,temp),但发现交换后两个结点后会和前两个结点脱节,所以一直失败
- 建议以后多使用虚拟头结点(dummyNode),不然每次都要对头结点进行单独讨论,很麻烦
- 其实想到以交换两个结点的前一个结点作为核心变量,思路就会清晰很多
- 代码随想录的递归版本很厉害,但我自己肯定想不到
本人代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummyHead = new ListNode(-1,head);
ListNode cur = dummyHead;
ListNode temp;
ListNode temp1;
while(cur.next != null && cur.next.next != null){
temp = cur.next;
temp1 = cur.next.next.next;
cur.next = temp.next;
cur.next.next = temp;
temp.next = temp1;
cur = temp;
}
return dummyHead.next;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
// 递归版
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummyHead = new ListNode(-1,head);
reverse(dummyHead);
return dummyHead.next;
}
public void reverse(ListNode cur){
if(cur.next == null || cur.next.next == null){
return;
}
ListNode temp = cur.next;
ListNode temp1 = cur.next.next.next;
cur.next = temp.next;
cur.next.next = temp;
temp.next = temp1;
reverse(temp);
}
}
代码随想录代码
// 虚拟头结点
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
ListNode prev = dummyNode;
while (prev.next != null && prev.next.next != null) {
ListNode temp = head.next.next; // 缓存 next
prev.next = head.next; // 将 prev 的 next 改为 head 的 next
head.next.next = head; // 将 head.next(prev.next) 的next,指向 head
head.next = temp; // 将head 的 next 接上缓存的temp
prev = head; // 步进1位
head = head.next; // 步进1位
}
return dummyNode.next;
}
}
// 递归版本
class Solution {
public ListNode swapPairs(ListNode head) {
// base case 退出提交
if(head == null || head.next == null) return head;
// 获取当前节点的下一个节点
ListNode next = head.next;
// 进行递归,swapPair函数就是把下一对节点进行交换并返回交换后的第一个节点,如果下一对是单个或者没有节点,则直接返回
ListNode newNode = swapPairs(next.next);
// 这里进行交换
next.next = head;
head.next = newNode;
return next;
}
}
19. 删除链表的倒数第 N 个结点
本人第一次写(暴力解法)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
// 算出总个数再指定下标
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
int count = 0;
ListNode dummyHead = new ListNode(-1,head);
ListNode cur = dummyHead;
while(cur != null){
cur = cur.next;
count++;
}
int targetIndex = count - n;
int index = 0;
cur = dummyHead;
while(index != targetIndex - 1){
cur = cur.next;
index++;
}
cur.next = cur.next.next;
return dummyHead.next;
}
}
思路
- 使用快、慢双指针,中间隔
n
个节点(因为要删除节点,则得有指向该节点的前一个结点的指针),想到这个就比较简单了
本人第二次写(双指针)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyHead = new ListNode(-1,head);
ListNode slow = dummyHead;
ListNode fast = dummyHead;
while(n-- >= 0){
fast = fast.next;
}
while(fast != null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummyHead.next;
}
}
代码随想录代码
public ListNode removeNthFromEnd(ListNode head, int n){
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
ListNode fastIndex = dummyNode;
ListNode slowIndex = dummyNode;
//只要快慢指针相差 n 个结点即可
for (int i = 0; i < n ; i++){
fastIndex = fastIndex.next;
}
while (fastIndex.next != null){
fastIndex = fastIndex.next;
slowIndex = slowIndex.next;
}
//此时 slowIndex 的位置就是待删除元素的前一个位置。
//具体情况可自己画一个链表长度为 3 的图来模拟代码来理解
slowIndex.next = slowIndex.next.next;
return dummyNode.next;
}
面试题 02.07. 链表相交
思路
-
求出两个链表的长度,并求出两个链表长度的差值,然后让curA移动到,和curB 末尾对齐的位置
-
比较curA和curB是否相同,如果不相同,同时向后移动curA和curB,如果遇到curA == curB,则找到交点。
否则循环退出返回空指针
时间复杂度:O(n + m)
本人代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA == null || headB == null){
return null;
}
ListNode curA = headA;
int countA = 1;
ListNode curB = headB;
int countB = 1;
while(curA.next != null){
curA = curA.next;
countA++;
}
while(curB.next != null){
curB = curB.next;
countB++;
}
int index;
ListNode indexA = headA;
ListNode indexB = headB;
if(countA > countB){
index = countA - countB;
while(index-- > 0){
indexA = indexA.next;
}
}else{
index = countB - countA;
while(index-- > 0){
indexB = indexB.next;
}
}
while(indexA != indexB){
indexA = indexA.next;
indexB = indexB.next;
}
if(indexB != indexA){
return null;
}
return indexA;
}
}
代码随想录代码
public 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) {
//1. swap (lenA, lenB);
int tmpLen = lenA;
lenA = lenB;
lenB = tmpLen;
//2. swap (curA, curB);
ListNode tmpNode = curA;
curA = curB;
curB = tmpNode;
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
while (gap-- > 0) {
curA = curA.next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != null) {
if (curA == curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
}
142. 环形链表 II
思路
判断是否有环?
使用快慢指针法,分别定义 fast 和 slow 指针,从头结点出发,fast指针每次移动两个节点,slow指针每次移动一个节点,如果 fast 和 slow指针在途中相遇 ,说明这个链表有环(因为fast相对于slow,每次都是追赶一步,所以一定会相遇,如果是两步,则可能会跳过slow,无法相遇)
如果有环,如何找到这个环的入口
(x + y) * 2 = x + y + n (y + z)
-> x = (n - 1) (y + z) + z
从头结点出发一个指针,从相遇节点 也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是 环形入口的节点
也就是在相遇节点处,定义一个指针index1,在头结点处定一个指针index2。
让index1和index2同时移动,每次移动一个节点, 那么他们相遇的地方就是 环形入口的节点。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ABOo5lyf-1668950522161)(代码随想录算法训练营第四天 203.移除链表元素、 707.设计链表 、206.反转链表.assets/008eGmZEly1goo58gauidg30fw0bi4qr.gif)]
那么 n如果大于1是什么情况呢,就是fast指针在环形转n圈之后才遇到 slow指针。
其实这种情况和n为1的时候 效果是一样的,一样可以通过这个方法找到 环形的入口节点,只不过,index1 指针在环里 多转了(n-1)圈,然后再遇到index2,相遇点依然是环形的入口节点。
为什么slow进入的第一圈就必定会遇到fast
本人代码
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode dummyHead = new ListNode(-1,head);
ListNode fast = dummyHead;
ListNode slow = dummyHead;
while(fast == dummyHead || fast != slow){
if(fast == null || fast.next == null){
return null;
}
fast = fast.next.next;
slow = slow.next;
// System.out.println("slow.val:" + slow.val);
// System.out.println("fast.val:" + fast.val);
}
// System.out.println(slow.val);
slow = dummyHead;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
代码随想录代码
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {// 有环
ListNode index1 = fast;
ListNode index2 = head;
// 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
while (index1 != index2) {
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
}
low.next;
fast = fast.next.next;
if (slow == fast) {// 有环
ListNode index1 = fast;
ListNode index2 = head;
// 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
while (index1 != index2) {
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
}