链表面试题常用的数据结构和技巧
- 使用容器(哈希表、数组等)
- 快慢指针
-
eg1. 找到一个链表的中间节点
-
eg2. 回文结构
- 笔试:全放到栈里,一个个弹出,从首节点开始比较
- 面试:链表的方法需要注意边界
-
eg3. 将单向链表按某值划分成左边小、中间相等、右边大的形式
- 笔试:把链表放入数组里,在数组上做partition
- 面试:分成大、中、小三部分,再把各个部分之间串起来
-
eg4. 一种特殊的单链表节点的描述如下
class Node {
int value;
Node next;
Node rand;
Node(int val) {value = val;}
}
rand指针是单链表节点结构中新增的指针,rand可能指向链表中的任意一个节点,也可能指向null。
给定一个有Node节点类型组成的无环单链表的头节点head,请实现一个函数完成这个链表的复制,并返回复制的新链表的头结点。【要求:时间复杂度为O(N),额外空间复杂度为O(1)】
- 思路需要人为构造一种新老节点之间的对应关系f(),使得newNode = f(oldNode),可以得到newNode.next = f(oldNode.next),newNode.rand = f(oldNode.rand)
- 使用一个Map<Node, Node>来存储<老节点,新节点>,先复制所有的节点,然后再一个循环连接对应的next和rand节点
public static Node copyListWithRand1(Node head) {
// key 老节点
// value 新节点
HashMap<Node, Node> map = new HashMap<Node, Node>();
Node cur = head;
while (cur != null) {
map.put(cur, new Node(cur.value));
cur = cur.next;
}
cur = head;
while (cur != null) {
// cur 老
// map.get(cur) 新
// 新.next -> cur.next克隆节点找到
map.get(cur).next = map.get(cur.next);
map.get(cur).rand = map.get(cur.rand);
cur = cur.next;
}
return map.get(head);
}
2. 将每个新复制生成的节点都放在原节点的后面curCopy.rand = cur.rand != null ? cur.rand.next:null;
然后进行一个split的操作对数组进行分离
public static Node copyListWithRand2(Node head) {
if (head == null) {
return null;
}
Node cur = head;
Node next = null;
// copy node and link to every node
// 1 -> 2
// 1 -> 1' -> 2
while (cur != null) {
// cur 老 next 老的下一个
next = cur.next;
cur.next = new Node(cur.value);
cur.next.next = next;
cur = next;
}
cur = head;
Node curCopy = null;
// set copy node rand
// 1 -> 1' -> 2 -> 2'
while (cur != null) {
// cur 老
// cur.next 新 copy
next = cur.next.next;
curCopy = cur.next;
curCopy.rand = cur.rand != null ? cur.rand.next : null;
cur = next;
}
// head head.next
Node res = head.next;
cur = head;
// split
while (cur != null) {
next = cur.next.next;
curCopy = cur.next;
cur.next = next;
curCopy.next = next != null ? next.next : null;
cur = next;
}
return res;
}
- eg5. 给定两个可能有环的单链表,头结点head1和head2,请实现一个函数,如果两个链表相交,请返回相交的第一个节点。如果不相交,返回null。【要求:如果两个链表长度之和为N,时间复杂度请达到O(N), 额外空间复杂度请达到O(1)
- 如何找到环的起点
- 用一个Set记录走过的节点,第一个重复的就是 O(N)
- 快慢指针
- head1,head2两个数组对应的环的起始节点分别是loop1和loop2[不存在为null]
- 重点:如果两个单链表一旦相交,从相交的节点开始,共享所有的节点
- -> 所以两个组数要么都没环,要么都有环(且公用这个环)
- 没环
- 简单的来说就是这两个数组共享了后N个节点
- 先让一个指着长数组的头节点的指针走两个数组的长度差步
- 然后两个数组同时往后指,直到两个节点相等
- 有环
- 在环前相交/同一个环入口:loop1 == loop2
- 不同的环入口:能从loop1在内圈内遇到loop2
- 没环
- 如何找到环的起点
// 找到链表第一个入环节点,如果无环,返回null
public static Node getLoopNode(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
// n1 慢 n2 快
Node slow = head.next; // n1 -> slow
Node fast = head.next.next; // n2 -> fast
while (slow != fast) {
if (fast.next == null || fast.next.next == null) {
return null;
}
fast = fast.next.next;
slow = slow.next;
}
// slow fast 相遇
fast = head; // n2 -> walk again from head
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
// 如果两个链表都无环,返回第一个相交节点,如果不相交,返回null
public static Node noLoop(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
Node cur1 = head1;
Node cur2 = head2;
int n = 0;
while (cur1.next != null) {
n++;
cur1 = cur1.next;
}
while (cur2.next != null) {
n--;
cur2 = cur2.next;
}
if (cur1 != cur2) {
return null;
}
// n : 链表1长度减去链表2长度的值
cur1 = n > 0 ? head1 : head2; // 谁长,谁的头变成cur1
cur2 = cur1 == head1 ? head2 : head1; // 谁短,谁的头变成cur2
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
// 两个有环链表,返回第一个相交节点,如果不相交返回null
public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
Node cur1 = null;
Node cur2 = null;
if (loop1 == loop2) { //环前或者环入口相交
cur1 = head1;
cur2 = head2;
int n = 0;
while (cur1 != loop1) {
n++;
cur1 = cur1.next;
}
while (cur2 != loop2) {
n--;
cur2 = cur2.next;
}
cur1 = n > 0 ? head1 : head2; // cur1用来存长的数组
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
} else { // 在环中相交或者不相交
cur1 = loop1.next;
while (cur1 != loop1) {
if (cur1 == loop2) {
return loop1;
}
cur1 = cur1.next;
}
return null;
}
}
public static Node getIntersectNode(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
Node loop1 = getLoopNode(head1);
Node loop2 = getLoopNode(head2);
if (loop1 == null && loop2 == null) {
return noLoop(head1, head2);
}
if (loop1 != null && loop2 != null) {
return bothLoop(head1, loop1, head2, loop2);
}
return null;
}
-
eg6. 不给单链表的头节点,只给想要删除的节点,如何在链表上删除该节点?结论:不行,一定要有头节点
-
注意如果删除最后一个节点c的时候,直接将c置为空,内存中b节点的next还是指向原本的c节点的