24. 两两交换链表中的节点
- 只要有断的位置就应该提前设置缓存量,将断点保存,还要注意要在循环里面设置
- ListNode dummy = new ListNode(0);
dummy.next = head;//定义虚拟头节点两句缺一不可!
/**
* 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 dummy = new ListNode(0);
dummy.next = head;
ListNode cur = dummy;
while (cur.next != null && cur.next.next != null) {//奇数和偶数情况的终止条件,顺序不能反,有先后关系
ListNode temp0 = cur.next;
ListNode temp1 = cur.next.next.next;//有断点的位置一定要提前设置临时缓存,还要注意要在循环里面设置
cur.next = cur.next.next;
cur.next.next = temp0;
temp0.next = temp1;
cur = cur.next.next;
}
return dummy.next;
}
}
19.删除链表的倒数第N个节点 10:38-11:06
- 快慢指针,快指针先走n步,随后快慢一起走,但是需要注意:删除元素必须找到该元素的上一个元素,因此快指针应先走n+1步
- return 要在pubilc{}里面
/**
* 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 dummy = new ListNode(0);
dummy.next = head;
ListNode fast = dummy;
ListNode slow = dummy;
for (int i = 0; i <= n; i++) {
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
面试题 02.07链表相交(题目绕蒙了)
题目
文章讲解
无视频讲解
思路:求两个链表交点节点的指针。注意,交点不是数值相等,而是指针相等。
/**
* 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) {
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0, lenB = 0;
while (curA != null) {// 求链表A长度
lenA++;
curA = curA.next;
}
while (curB != null) {
lenB++;
curB = curB.next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
// 交换lenA与lenB
int tmplen = lenA;
lenA = lenB;
lenB = tmplen;
// 交换curA与curB
ListNode tmpNode = curA;
curA = curB;
curB = tmpNode;
}
// 求长度
int gap = lenA - lenB;
// 让curA与curB在同一起点
for (int i = 0; i < gap; i++) {
curA = curA.next;
}
// 遍历curA与curB,遇到相同直接返回
while (curA != null) {
if (curA == curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
}
142.环形链表II
题目
文章讲解
视频讲解
思路:
slow=x+y
fast=x+y+n(y+z)
2slow=fast
当n=1,x=z
/**
* 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 fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {//只检查fast就可以了
fast = fast.next.next;
slow = slow.next;
if (slow == fast) {
ListNode index1 = fast;
ListNode index2 = head;
while (index1 != index2) {//x=z
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
}