目录
一、问题总纲领
·笔试:一切为了时间复杂度,不用太关注空间复杂度;一般来说优先考虑用数组,hash表等解决
·面试:既要考虑时间复杂度,又要考虑空间复杂度;一般用指针解决
二、例题
(一)、链表中点
⚪ 链表奇数长度,返回中点,偶数长度,返回上中点
⚪ 链表奇数长度,返回中点,偶数长度,返回下中点
⚪ 链表奇数长度,返回中点前一个,偶数长度,返回上中点前一个
⚪ 链表奇数长度,返回中点前一个,偶数长度,返回下中点前一个
(1)笔试
笔试不考虑空间复杂度,可以用数组求中点:
链表结构:
public class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
}
遍历链表每个节点,放入数据,然后用数据特性求中点:
//1获取中点或者上中点 -- 笔试
public Node getMidOrUpMidWrit(Node head){
Node node = head;
ArrayList<Node> arrayList = new ArrayList();
while(node.next != null){
arrayList.add(node);
node = node.next;
}
return arrayList.get(arrayList.size()/2);
}
//2获取中点或者下中点 -- 笔试
public Node getMidOrLowMidWrit(Node head){
Node node = head;
ArrayList<Node> arrayList = new ArrayList();
while(node.next != null){
arrayList.add(node);
node = node.next;
}
return arrayList.get(arrayList.size()%2 == 0 ? arrayList.size()/2 :arrayList.size()/2+1);
}
//3获取中点或者上中点前一个 -- 笔试
public Node getMidOrUpMidPreWrit(Node head){
Node node = head;
ArrayList<Node> arrayList = new ArrayList();
while(node.next != null){
arrayList.add(node);
node = node.next;
}
return arrayList.get(arrayList.size()/2 - 1);
}
//4获取中点或者下中点前一个 -- 笔试
public Node getMidOrUpMidNextWrit(Node head){
Node node = head;
ArrayList<Node> arrayList = new ArrayList();
while(node.next != null){
arrayList.add(node);
node = node.next;
}
return arrayList.get(arrayList.size()%2 == 0 ? arrayList.size()/2-1 :arrayList.size()/2);
}
(2)面试
利用指针求中点,一般来说,最多用两个指针就可以解决链表问题。
fast指针一次跳2个节点,slow指针一次跳一个节点。那么fast到最后一共跳了2x+1个节点,slow跳了x+1个节点。
(2-1)如果链表是奇数,说明fast指针在最后停止,一共2x+1个节点,那么中点是 x+1,即slow指针所在位置;如果链表是偶数,说明fast指针停止的位置后面还有一个节点,链表个数是2x+2个,上中点在x+1处
//1获取中点或者上中点 -- 面试
public Node getMidOrUpMidIn(Node head){
Node slow = head;
Node fast = head;
while(fast.next !=null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
(2-2)中点或者下中点要看fast后面有没有节点,如果没有,说明是奇数,slow就停止,如果有,说明是偶数,slow还要+1才为下中点
//2获取中点或者下中点 -- 面试
public Node getMidOrLowMidIn(Node head){
Node slow = head;
Node fast = head;
while(fast.next !=null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}
return fast.next !=null ? slow.next : slow;
}
(2-3)
//3获取中点或者上中点前一个 -- 面试
public Node getMidOrUpMidPreIn(Node head){
Node slow = head;
Node fast = head;
Node tmp = head;
while(fast.next !=null && fast.next.next != null){
fast = fast.next.next;
tmp = slow;
slow = slow.next;
}
return tmp;
}
(2-4)
//4获取中点或者下中点前一个 -- 面试
public Node getMidOrUpMidNextIn(Node head){
Node slow = head;
Node fast = head;
Node tmp = head;
while(fast.next !=null && fast.next.next != null){
fast = fast.next.next;
tmp = slow;
slow = slow.next;
}
return fast.next == null ? tmp : slow;
}
(二)、回文结构
给定一个单链表的头节点head,请判断该链表是否为回文结构。
(1)笔试
利用栈即可,把链表依次放入数组,依次弹出与链表对比
//判断单链表是否为回文结构 -- 笔试1
public boolean isPalindromeIn1(Node head){
if(head == null){
return false;
}
Node cur = head;
//入栈
Stack<Node> stack = new Stack<>();
while (cur != null){
stack.add(cur);
cur = cur.next;
}
cur = head;
//出栈并比较
while (cur != null){
if(stack.pop().value != cur.value){
return false;
}
cur = cur.next;
}
return true;
}
当然,也可以减少栈的大小,从中点或者下中点开始放入栈,然后依次弹出
//判断单链表是否为回文结构 -- 笔试2
public static boolean isPalindromeIn2(Node head){
if(head == null){
return false;
}
//获取中点或者上中点
FastAndSlow fastAndSlow = new FastAndSlow();
Node mid = fastAndSlow.getMidOrUpMidIn(head);
Node cur = mid.next;
//入栈
Stack<Node> stack = new Stack<>();
while (cur != null){
stack.add(cur);
cur = cur.next;
}
cur = head;
//出栈并比较
while (stack.size() > 0){
if(stack.pop().value != cur.value){
return false;
}
cur = cur.next;
}
return true;
}
(2)面试
利用指针,可以把单链表,从中点或者上中点开始到最后的节点那一部分指针反向,然后左右两部分指针移动依次对比,最后把反向部分继续反向成原链表。
链表反向:
//反转链表
public static Node reverse(Node cur){
//需要三个指针,循环中间指针,中间指针指向前一个指针,记录后一个指针,保证后面值可以找到
Node n1 = cur.next;
cur.next = null;
Node n2;
while(n1 != null){
n2 = n1.next;
n1.next = cur;
cur = n1;
n1 = n2;
}
return cur;
}
左右两部分对比:
//判断单链表是否为回文结构 -- 面试
public static boolean isPalindromeWrit(Node head){
//获取中点或者上中点
FastAndSlow fastAndSlow = new FastAndSlow();
Node mid = fastAndSlow.getMidOrUpMidIn(head);
//中点(上中点) - 最后 进行指针反转
Node right = Reserve.reverse(mid);
Node tmp = right;
Node left = head;
while(left != right && left != null){
if(left.value != right.value){
Reserve.reverse(tmp);
return false;
}
right = right.next;
left = left.next;
}
Reserve.reverse(tmp);
return true;
}
(三)、链表分成大中小区
把单向链表,按照某值划分成左边小,中间等,右边大的形式
(1)笔试
无非是荷兰国旗问题,但是荷兰国旗针对数组,所以我们先把链表转成数组,然后进行partition,之后再把数组转成链表。
荷兰国旗partition :
//荷兰国旗
public static void partition(List<Integer> list,Integer value){
if(list == null || list.size() == 0){
return;
}
int cur = 0,small = cur-1;
int large = list.size()-1;
while(cur != large){
if(list.get(cur) > value){
swap(list,cur,large--);
}else if(list.get(cur) < value){
swap(list,cur++,++small);
}else{
cur ++;
}
}
}
链表->数组,数组->链表
//链表分成大中小区 -- 笔试
public static Node linkedPartition(Node head,int value){
Node node = head;
ArrayList<Integer> arrayList = new ArrayList<>();
//链表->数组
while(node != null){
arrayList.add(node.value);
node = node.next;
}
ListUtil.partition(arrayList,value);
//list变成node链表
Node node2 = new Node(arrayList.get(0));
Node headFinal = node;
for(int i=1;i<arrayList.size();i++){
node2.next = new Node(arrayList.get(i));
node2 = node2.next;
}
return headFinal;
}
(2)面试
利用指针,用6个指针,分别记录小于区的头尾,等于区的头尾,大于区的头尾
例如链表如下,与2比较
a.首先1,小于2,放入小于区,小于区的头指针记录为1,尾指针记录为1
b.然后是2,等于2,放入等于区,等于区的头指针记录为2,尾指针记录为2
c.然后是3,大于2,放入大于区,大于区的头指针记录为3,尾指针记录为3
d.然后是5,大于2,放入大于区,大于区的头指针仍为3,尾指针的下一个记录为5,尾指针后移
e.然后是2,等于2,放入等于区,等于区的头指针仍为2,尾指针的下一个记录为2,尾指针后移
f.然后是9,大于2,放入大于区,大于区的头指针仍为3,尾指针的下一个记录为9,尾指针后移
最后,小于区的尾指针连等于区的头指针,等于区的尾指针连大于区的头指针,当然注意如果头或者尾为空情况
//链表分成大中小区 -- 面试
public static Node linkedPartitionIn(Node head,int value){
Node sH = null;
Node sT = null;
Node eH = null;
Node eT = null;
Node lH = null;
Node lT = null;
Node node =head;
while(node != null){
if(node.value < value){
if(sH == null){
sH = node;
sT = node;
}else{
sT.next = node;
sT = sT.next;
}
}else if(node.value == value){
if(eH == null){
eH = node;
eT = node;
}else{
eT.next = node;
eT = eT.next;
}
}else{
if(lH == null){
lH = node;
lT = node;
}else{
lT.next = node;
lT = lT.next;
}
}
node = node.next;
}
if(sT != null){
sT.next = eH == null ? (lH == null ? null : lH) : eH;
}
if(eT != null){
eT.next = lH;
}
return sH == null ? (eH == null ? lH : eH) : sH;
}
(四)、复制有随机指针的链表
有一种特殊链表:
public class Node{
public int value;
public Node next;
public Node rand;
public Node(int value) {
this.value = value;
}
}
rand可以指向链表中任意节点,也可以指向null。给定一个此Node类型的无环单链表头节点,要求完成此链表复制。
(1)笔试
利用容器,虽然可以把这个链表的每一个节点放到list里面,但是如果用循环list的方式给新node复制是不可取的,因为循环体里面必须是新建Node对象,这个不现实,所以我们希望事先生成和老链表同样数量的新node,所以用hashmap
//复制有rand指针的node -- 笔试
public Node copyIn(Node head){
HashMap<Node,Node> hashMap = new HashMap<>();
Node node = head;
//循环node,放入map
while (node != null){
hashMap.put(node,new Node(node.value));
node = node.next;
}
node = head;
while (node != null){
hashMap.get(node).next = hashMap.get(node.next);
hashMap.get(node).rand = hashMap.get(node.rand);
node = node.next;
}
return hashMap.get(head);
}
(2)面试
把上述链表,转成在每个节点中间插入一个新节点:
新节点的next是老节点的.next.next;新节点的rand是老节点的.rand.next
//复制有rand指针的node -- 面试
public Node copyWrite(Node head){
Node node = head;
Node cur = null;
while (node != null) {
cur = node.next;
node.next = new Node(node.value);
node.next.next = cur;
node = cur;
}
node = head;
//设置好rand
while(node != null){
cur = node.next.next;
node.next.rand = node.rand == null ? null : node.rand.next;
node = cur;
}
//设置好next
node = head;
Node nodeCopy = null;
while (node != null){
cur = node.next.next;
nodeCopy = node.next;
nodeCopy.next = cur== null ? null:cur.next;
node.next = cur;
node = cur;
}
return head.next;
}
(五)、有/无环 链表相交
两个可能有环可能无环的单链表,头节点head1和head2,
如果两个表相交,返回相交的第一个节点,如果不相交,返回null
(五-1)、首先判断一个链表是否有环
(1)笔试
利用list容器,依次遍历node,如果list.contains(node)为true,那么就说明有环,返回此node,如果为false,就把这个node加到list里,继续下一个node
//判断一个链表是是否有环 -- 笔试
public static Node isCircular(Node head){
ArrayList<Node> arrayList = new ArrayList<>();
Node node = head;
while(node != null){
if(arrayList.contains(node)){
return node;
}
arrayList.add(node);
node = node.next;
}
return null;
}
(2)面试
对于有环的单链表,有一个指针规则:一个快指针每次走两个节点,一个慢指针每次走一个节点,两者会有相遇,第一次相遇之后,快指针返回头节点,然后每次走一个节点,两者会再次在环节点相遇
第一次相遇,在6节点
然后f指针回到头部,一次走一个,两者最终在环的节点相遇
//判断一个链表是是否有环 -- 面试
public static Node isCircularIn(Node head){
if(head == null || head.next == null || head.next.next == null){
return null;
}
Node slow = head.next;
Node fast = head.next.next;
while(fast != null){
if(slow == fast){
//有环
fast = head;
while (slow != fast){
slow = slow.next;
fast = fast.next;
}
return fast;
}
fast = fast.next == null ? null : fast.next.next;
slow = slow.next;
}
return null;
}
(五-2)、链表相交
首先A链表可能有环可能无环,B链表可能有环,可能无环。那么就有4中情况,
1、A无环 B无环
2、A有环 B无环
3、A无环 B有环
4、A有环 B有环
而情况2 3 不可能相交,所以只看1 4就可以。
(1)笔试
笔试利用容器,A放到listA里面,遍历B的节点,如果B的节点在listA中,那么就返回当前节点,
如果B的节点遍历了一遍(node为null或者B放入的listB有此节点)还是没有在listA中,则就是不相交
//两个链表相交 -- 笔试
public static Node getIntersectionWrite(Node head1,Node head2){
if((isCircularIn(head1) == null && isCircularIn(head2) != null) || (isCircularIn(head1) != null && isCircularIn(head2) == null)){
return null;
}
Node node1 = head1;
Node node2 = head2;
ArrayList<Node> arrayList1 = new ArrayList<>();
ArrayList<Node> arrayList2 = new ArrayList<>();
while(node1 != null){
if(arrayList1.contains(node1)){
break;
}
arrayList1.add(node1);
node1 = node1.next;
}
while (node2 != null){
if(arrayList1.contains(node2)){
return node2;
}else if(arrayList2.contains(node2)){
return null;
}
arrayList2.add(node2);
node2 = node2.next;
}
return null;
}
(2)面试
1、A无环 B无环
有这两种情况
如果链表1和链表2的尾节点不等,说明不相交。如果相等,说明相交,求相交时,记录链表1的长度是a,链表2的长度是b,长链表减去a b的差值之后,再同时和短链表 指针往下走,两者会在交点相遇
private static Node noLoop(Node head1, Node head2){
if(head1 == null || head2 == null){
return null;
}
//循环获取两个链表到尾节点
Node node1 = head1;
Node node2 = head2;
Node end1 = null;
Node end2 = null;
int len = 0;
while(node1 != null){
end1 = node1;
len ++;//记录node1链表的长度
node1 = node1.next;
}
while(node2 != null){
end2 = node2;
len -- ;//记录node1和node2链表的长度差值
node2 = node2.next;
}
//尾部节点不相等,说明不相交
if(end1 != end2){
return null;
}
//尾节点相等,说明相交
node1 = head1;
node2 = head2;
int lenPositive = Math.abs(len);
//1 2链表距离到相交节点一样的长度
if(len > 0){//1号链长
for(int i=0;i<len;i++){
node1 = node1.next;
}
}else{//2号链长或者相等
for(int i=0;i<lenPositive;i++){
node2 = node2.next;
}
}
while (node1 != node2){
node1 = node1.next;
node2 = node2.next;
}
return node1;
}
4、A有环 B有环
有四种情况:
其中情况C和D其实是一样的,都可以用判断无环链表是否相交的方法。
对于情况A和B,可以循环链表1的环,如果在遇到自己入环节点之前遇到了链表2的入环节点,说明是情况B,否则是情况A
private static Node bothLoop(Node head1,Node head2,Node loop1,Node loop2){
if(head1 == null || head2 == null){
return null;
}
if(loop1 == loop2){ //两个环,入环节点一致,可能清楚 C或者D,无论哪一种,都可以用noLoop方法
return noLoop(head1,head2);
}
//连个环入环节点不一致,有可能 A或者B,先判断是否相交
Node node1 = loop1.next;
while(node1 != loop2){
if(node1 == loop1){//情况A
return null;
}
node1 = node1.next;
}
//情况B
return loop1;
}
总体,调用:
//两个链表相交 -- 面试
public static Node getIntersectionIn(Node head1,Node head2){
Node loop1 = isCircularIn(head1);
Node loop2 = isCircularIn(head2);
if((loop1 == null && loop2 != null) || (loop1 != null && loop2 == null)){
return null;
}
if(loop1 == null && loop2 == null){//无环链表比较
return noLoop(head1,head2);
}else{//都有环
return bothLoop(head1,head2,loop1,loop2);
}
}
(六)、删除某节点
能否不给单链表的头节点,只给要删除的节点即可删除此节点。
抖机灵做法,把下一个节点的赋值给要删除的节点,要删除的节点.next指向它一个的下一个节点
但是有问题,如果每个节点都是服务器,你不能把对外提供服务的④节点删除;
此外,如果要删除的节点是最后一个节点,例如删除⑤,它的下一个节点是null,把它赋值成null,并不是删除它,它的前一个节点要指向null的内存区域才是删除⑤.
null是内存中特定的区域。
例如:
public static void main(String[] args) {
Node a = new Node(1);
Node b = new Node(2);
a.next = b;
}
new一个节点的时候,是在内存中申请了一个区域,里面放着1,此时这个区域的next指针悬空
然后a的引用指向这个内存区域,
然后再申请一个区域,里面放着2,
b的引用指向2,
b.next = a是 1的那块区域.next 指向2的那块区域
此时让b = null,是说b 不指向2了,b指向了null的区域,但是1->2仍然在内存中存在。
所以,如果想删除链表中的某节点,必须给head节点才可以。