第一题
问题描述:快慢指针
1)输入链表头节点,奇数长度返回中点,偶数长度返回上中点
2)输入链表头节点,奇数长度返回中点,偶数长度返回下中点
3)输入链表头节点,奇数长度返回中点前一个,偶数长度返回上中点前一个
4)输入链表头节点,奇数长度返回中点前一个,偶数长度返回下中点前一个
提示:
求上中点,当前结点后有两个结点才继续跳
求下中点,当前结点后有一个结点就继续跳
/**
* 问题描述:快慢指针
* <p>
* 1)输入链表头节点,奇数长度返回中点,偶数长度返回上中点
* 2)输入链表头节点,奇数长度返回中点,偶数长度返回下中点
* 3)输入链表头节点,奇数长度返回中点前一个,偶数长度返回上中点前一个
* 4)输入链表头节点,奇数长度返回中点前一个,偶数长度返回下中点前一个
*/
public class LinkedListMid {
public static class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
}
//1.中点或上中点
public static Node midOrUpMidNode(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return head;
}
Node slow = head.next;//慢指针
Node fast = head.next.next;//快指针
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
//2.中点或下中点
public static Node midOrDownMidNode(Node head) {
if (head == null || head.next == null) {
return head;
}
Node slow = head.next;
Node fast = head.next.next;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
//3.返回中点前一个或上中点前一个(slow少走一布)
public static Node midOrUpMidPreNode(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
Node slow = head;
Node fast = head.next.next;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
//4.返回中点前一个或下中点前一个
public static Node midOrDownMidPreNode(Node head) {
if (head == null || head.next == null) {
return null;
}
Node slow = head;
Node fast = head.next.next;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}
第二题
问题描述:是否是回文结构。给定一个单链表的头节点head,请判断该链表是否为回文结构。
例如:12321 √ 12312×
提示:1 -> 2 -> 3 <-2 <- 1
↓
null
package com.lzf2.class08;
import java.util.Stack;
//是否是回文结构。给定一个单链表的头节点head,请判断该链表是否为回文结构。
public class IsPalindromeList {
public static class Node{
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
}
// need n extra space
public static boolean isPalindrome1(Node head) {
Stack<Node> stack = new Stack<Node>();
Node cur = head;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
while (head != null) {
if (head.value != stack.pop().value) {
return false;
}
head = head.next;
}
return true;
}
// need n/2 extra space
public static boolean isPalindrome2(Node head) {
if (head == null || head.next == null) {
return true;
}
Node right = head.next;
Node cur = head;
while (cur.next != null && cur.next.next != null) {
right = right.next;
cur = cur.next.next;
}
Stack<Node> stack = new Stack<Node>();
while (right != null) {
stack.push(right);
right = right.next;
}
while (!stack.isEmpty()) {
if (head.value != stack.pop().value) {
return false;
}
head = head.next;
}
return true;
}
// need O(1) extra space
public static boolean isPalindrome3(Node head) {
if (head == null || head.next == null){
return true;
}
//1.快慢指针找到链表的中间位置
Node slow = head;//中点
Node fast = head;
while (fast.next != null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}
//2.调整链表结构
Node curr = slow.next;
slow.next = null;
while (curr != null){
Node next = curr.next;
curr.next = slow;
slow = curr;
curr = next;
}
//curr = null,slow在最后一个
//3.进行比较,注意slow不要动,后面需要把链表重新调整回来.
Node R = slow;
Node L = head;
boolean res = true;
while (L != null && R != null){
if(L.value != R.value){
res = false;
break;//先不结束,需要把链表调整回来
}
L = L.next;
R = R.next;
}
//尾slow
curr = slow;
Node pre = null;
while (curr != null){
Node next = curr.next;
curr.next = pre;
pre = curr;
curr = next;
}
return res;
}
public static void printLinkedList(Node node) {
System.out.print("Linked List: ");
while (node != null) {
System.out.print(node.value + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = null;
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(2);
head.next.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
}
}
第三题
问题描述:将单向链表按某值划分成左边小、中间相等、右边大的形式
提示:根据值的大小分成,小于表、等于表、大于表。分别记录表头和表尾。
最后把三个链表串起来
//将单向链表按某值划分成左边小、中间相等、右边大的形式
public class SmallerEqualBigger {
//node结点
public static class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
}
public static Node listPartition(Node head, int pivot) {
Node sH = null;//small head
Node sT = null;//small tail
Node eH = null;//equal head
Node eT = null;//equal tail
Node mH = null;//big head
Node mT = null;//big tail
Node cur = head;
while (cur != null) {
Node next = cur.next;
cur.next = null;
if (cur.value > pivot) {
if(mH == null){
mH = cur;
mT = cur;
}else {
mT.next = cur;
mT = cur;
}
} else if (cur.value < pivot) {
if (sH == null){
sH = cur;
sT = cur;
}else {
sT.next = cur;
sT = cur;
}
} else {
if (eH == null){
eH = cur;
eT = cur;
}else {
eT.next = cur;
eT = cur;
}
}
cur = next;
}
//三个区拼起来
if (sT != null){
sT.next = eH;
eT = eH == null ? sT : eT;
}
if (eT != null){
eT.next = mH;
}
return sH != null ? sH : (eH != null ?eH : mH);
}
public static void printLinkedList(Node node) {
System.out.print("Linked List: ");
while (node != null) {
System.out.print(node.value + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head1 = new Node(7);
head1.next = new Node(9);
head1.next.next = new Node(1);
head1.next.next.next = new Node(8);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(2);
head1.next.next.next.next.next.next = new Node(5);
printLinkedList(head1);
// head1 = listPartition1(head1, 4);
head1 = listPartition(head1, 5);
printLinkedList(head1);
}
}
第四题
问题描述:
一种特殊的单链表节点类描述如下
class Node {
int value;
Node next;
Node rand;
Node(int val) { value = val; }
}
rand指针是单链表节点结构中新增的指针,rand可能指向链表中的任意一个节点,也可能指向null。
给定一个由Node节点类型组成的无环单链表的头节点 head,请实现一个函数完成这个链表的复制,并返回复制的新链表的头节点。
【要求】
时间复杂度O(N),额外空间复杂度O(1)
提示:复制每个结点,分别放在原结点后面。然后连上rand指针,最后去掉原结点的指针。
import java.util.HashMap;
//复制一个特殊的链表(有random指针)
public class CopyListWithRandom {
public static class Node {
int value;
Node next;
Node rand;
public Node(int value) {
this.value = value;
}
}
public static Node copyListWithRand2(Node head) {
if (head == null){
return null;
}
//1.copy每个新结点分别放在老结点后面
Node cur = head;
while (cur != null){
Node next = cur.next;
cur.next = new Node(cur.value);
cur.next.next = next;
cur = next;
}
//2.一次遍历两个结点,新结点的random的指针连上
cur = head;
while (cur != null){
cur.next.rand = cur.rand != null ? cur.rand.next : null;
cur = cur.next.next;
}
//3.把新结点 跟 老结点 分离开来
Node res = head.next;
cur = head;
while (cur != null){
Node next = cur.next.next;
Node copy = cur.next;
cur.next = next;
copy.next = next != null ? next.next : null;
cur = next;
}
return res;
}
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);
}
public static void printRandLinkedList(Node head) {
Node cur = head;
System.out.print("order: ");
while (cur != null) {
System.out.print(cur.value + " ");
cur = cur.next;
}
System.out.println();
cur = head;
System.out.print("rand: ");
while (cur != null) {
System.out.print(cur.rand == null ? "- " : cur.rand.value + " ");
cur = cur.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = null;
Node res1 = null;
Node res2 = null;
printRandLinkedList(head);
res1 = copyListWithRand1(head);
printRandLinkedList(res1);
res2 = copyListWithRand2(head);
printRandLinkedList(res2);
printRandLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
head.rand = head.next.next.next.next.next; // 1 -> 6
head.next.rand = head.next.next.next.next.next; // 2 -> 6
head.next.next.rand = head.next.next.next.next; // 3 -> 5
head.next.next.next.rand = head.next.next; // 4 -> 3
head.next.next.next.next.rand = null; // 5 -> null
head.next.next.next.next.next.rand = head.next.next.next; // 6 -> 4
System.out.println("原始链表:");
printRandLinkedList(head);
System.out.println("=========================");
res1 = copyListWithRand1(head);
System.out.println("方法一的拷贝链表:");
printRandLinkedList(res1);
System.out.println("=========================");
res2 = copyListWithRand2(head);
System.out.println("方法二的拷贝链表:");
printRandLinkedList(res2);
System.out.println("=========================");
System.out.println("经历方法二拷贝之后的原始链表:");
printRandLinkedList(head);
System.out.println("=========================");
}
}
第五题
给定两个可能有环也可能无环的单链表,头节点head1和head2。请实现一个函数,如果两个链表相交,请返回相交的 第一个节点。如果不相交,返回null
【要求】
如果两个链表长度之和为N,时间复杂度请达到O(N),额外空间复杂度 请达到O(1)。
提示:
两个链表情况:1)都无环 2)都有环 3)一个有环一个没环
先判断链表是否有环
对第1和第2种情况提供处理方法找到相交结点,第3种情况一定没有相交点
/**
* 给定两个可能有环也可能无环的单链表,头节点head1和head2。请实现一个函数,如果两个链表相交,请返回相交的 第一个节点。如果不相交,返回null
* 【要求】
* 如果两个链表长度之和为N,时间复杂度请达到O(N),额外空间复杂度 请达到O(1)。
*/
public class FindFirstIntersectNode {
public static class Node{
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
}
//返回两个链表的相交结点,不相交返回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;
}
//找到两个链表第一个入环结点,如果无环返回null。
public static Node getLoopNode(Node head){
if(head == null || head.next == null || head.next.next == null){
return null;
}
//快慢指针
Node fast = head.next.next;
Node slow = head.next;
while (fast != slow){
if(fast.next == null || fast.next.next == null){
return null;
}
fast = fast.next.next;
slow = slow.next;
}
//fast 和 slow 遇上了
fast = head;
while (fast != slow){
fast = fast.next;
slow = slow.next;
}
return slow;
}
//处理两个都无环的情况
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步)
cur1 = n > 0 ? head1 : head2;//长的
cur2 = cur1 == head1 ? head2 : head1;//短的
n = Math.abs(n);
while (n != 0){
cur1 = cur1.next;
n--;
}
//两个链表一起走
while (cur1 != cur2){
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
//处理两个都有环的情况
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;//长
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
//走差值步
while (n != 0){
cur1 = cur1.next;
n--;
}
//两个一起走
while (cur1 != cur2){
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}else {
Node cur = loop1.next;
while (cur != loop1){//走一圈
if(cur == loop2){
return loop1;
}
cur = cur.next;
}
return null;//两个环不相交
}
}
public static void main(String[] args) {
// 1->2->3->4->5->6->7->null
Node head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(7);
// 0->9->8->6->7->null
Node head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println(getIntersectNode(head1, head2).value);
// 1->2->3->4->5->6->7->4...
head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(7);
head1.next.next.next.next.next.next = head1.next.next.next; // 7->4
// 0->9->8->2...
head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next; // 8->2
System.out.println(getIntersectNode(head1, head2).value);
// 0->9->8->6->4->5->6..
head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println(getIntersectNode(head1, head2).value);
}
}
第六题
问题描述:不用头结点,删除某个结点
可以实现,但是不是真正的删除结点,而是只是做了值的更换,在某些场景下会出问题的。
而且删除不了最后一个结点。
public static void delete(Node node){
if(node == null){
return;
}
Node next = node.next;
if(next == null){//当前结点是最后一个结点,没办法删除
return;
}
node.value = next.value;
node.next = next.next;
}