今天的题感觉也都是链表基础水平。总体而言不难。
原题链接: 24 两两交换链表中的节点
没什么好说的,上来设置一个虚拟头结点狠狠迭代了。看题解还有一种递归的写法,实在想不到。
迭代写法:
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode cur = head, pre = dummy;
while (cur != null && cur.next != null) {
ListNode tmp = cur.next;
cur.next = tmp.next;
tmp.next = cur;
pre.next = tmp;
pre = cur;
cur = cur.next;
}
return dummy.next;
}
}
递归写法(官方解法):
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = head.next;
head.next = swapPairs(newHead.next);
newHead.next = head;
return newHead;
}
}
原题链接:19 删除链表的倒数第N个结点
这题也是一顿迭代结束。先遍历一遍找到结点总数,然后再次遍历到要删除结点的前一个结点,修改其指针即可。
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
int len = 0;
ListNode tmp = head;
while (tmp != null) {
len++;
tmp = tmp.next;
}
ListNode cur = dummy;
for (int i = 1; i < len - n + 1; i++) {
cur = cur.next;
}
cur.next = cur.next.next;
ListNode ans = dummy.next;
return ans;
}
}
另外官解的栈方法稍有不同,一并附上。本质上也是两趟遍历。
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
Deque<ListNode> stack = new LinkedList<>();
ListNode cur = dummy;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
for (int i = 0; i < n; ++i) {
stack.pop();
}
ListNode pre = stack.peek();
pre.next = pre.next.next;
return dummy.next;
}
}
原题链接: 160 相交链表
最简单的做法无过于用一个哈希集合存储链表A的每个结点,然后遍历链表B的每个结点,如果已经存在,那么这个交点就是这个已存在的结点。
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Set<ListNode> set = new HashSet<>();
ListNode cur = headA;
while (cur != null) {
set.add(cur);
cur = cur.next;
}
cur = headB;
while (cur != null) {
if (set.contains(cur)) return cur;
cur = cur.next;
}
return null;
}
}
官解的双指针做法非常有启发。
如果两链表相交,且不相交部分长度相等情况下,两指针在第一轮遍历时就会指向同一结点并将其返回;而在相交部分长度不相等的情况下,两指针会在完整遍历完一次链表后同时到达链表相交点并将其返回。
如果两链表不相交,且链表长度相等时,会出现完整遍历一次链表后pA == pB == null,即两指针同时指向null的情况,那么返回的就是null;链表长度不相等时,两指针就都会完整遍历完两个链表,最后出现的也是pA == pB == null,返回的还是null。
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) return null;
ListNode pA = headA, pB = headB;
while (pA != pB) {
pA = pA == null ? headB : pA.next;
pB = pB == null ? headA : pB.next;
}
return pA;
}
}
原题链接:142 环形链表
首先容易想到的也是一次遍历+哈希集合的方法。执行用时较长。
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode pos = head;
Set<ListNode> set = new HashSet<>();
while (pos != null) {
if (set.contains(pos)) return pos;
else {
set.add(pos);
pos = pos.next;
}
}
return null;
}
}
然后就是双指针解法,首先利用快慢指针先来判断链表中是否存在环。慢指针每次移动到后面一个结点,快指针移动到后面第二个结点,如果两指针相遇,那么说明有环。在存在环时,另外设置一个指针从head开始,与slow指针同时逐一遍历,相遇时所在的结点即链表中环的第一个结点。
public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) return null;
ListNode slow = head, fast = head;
while (fast != null) {
slow = slow.next;
if (fast.next != null) fast = fast.next.next;
else return null;
if (fast == slow) {
ListNode cur = head;
while (cur != slow) {
cur = cur.next;
slow = slow.next;
}
return cur;
}
}
return null;
}
}