24.两两交换链表中的结点
- 标签:链表、双指针
- 难度:4.5
有了前面的经验,这题就不算难了。面对这种复杂的链表操作,应该想到引入虚拟头结点。
核心步骤是交换的实现,只要保证一个原则:不断链。
经过分析,需要三个指针,分别指向待交换的两个结点和他俩前面的结点。
head是没用的,可以作为遍历指针使用,因为最后返回的是preHead.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) {
// 考虑只有一个结点或head为null的情况
if(head == null || head.next == null) {
return head;
}
// 引入虚拟头结点
ListNode preHead = new ListNode(0,head);
ListNode prev = preHead;
ListNode temp;
while(prev.next != null && prev.next.next != null){
temp = prev.next.next;
// 核心交换代码
prev.next = head.next;
head.next = head.next.next;
prev.next.next = head;
// 更新两个指针
prev = prev.next.next;
head = head.next;
}
return preHead.next;
}
}
方法二:递归
又到了每日的一看就会一写就废的时间了,我就很想知道这怎么想出来的。
// 递归版本
class Solution {
public ListNode swapPairs(ListNode head) {
// 出口
if(head == null || head.next == null) return head;
// 获取当前节点的下一个节点
ListNode next = head.next;
// 进行递归
ListNode newNode = swapPairs(next.next);
// 这里进行交换
next.next = head;
head.next = newNode;
return next;
}
}
19.删除链表的倒数第N个节点
- 标签:链表、双指针
- 难度:4.5
很经典的一道题,很容易想到双指针,先让快指针跑n个,再两个一块跑到终点,慢指针就可以删倒数第n个了。
唯一的易错点在于循环边界条件,可以画个图模拟一下。
public ListNode removeNthFromEnd(ListNode head, int n){
ListNode preHead = new ListNode(0);
preHead.next = head;
ListNode fastIndex = preHead;
ListNode slowIndex = preHead;
//只要快慢指针相差 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 preHead.next;
}
面试题 02.07. 链表相交
- 标签:链表
- 难度:5.5
很多种做法的一道题,需要尽可能降低复杂度。注意结点相同,不是数值相同。
代码随想录的做法还是比较好理解的。这个代码的书写风格在有些细节上我不习惯,自己写的时候我改了改,但我懒得贴那个版本了,先凑合看。
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
- 标签:链表
- 难度:5.0
时间关系,先贴代码,后面再看。
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;
}