链表问题
链表面试题常用数据结构和技巧 1)使用容器(哈希表、数组等) 2)快慢指针
1 快慢指针
1)输入链表头节点,奇数长度返回中点,偶数长度返回上中点 2)输入链表头节点,奇数长度返回中点,偶数长度返回下中点 3)输入链表头节点,奇数长度返回中点前一个,偶数长度返回上中点前一个 4)输入链表头节点,奇数长度返回中点前一个,偶数长度返回下中点前一个
public static class Node{
private Node next;
private int value;
public Node(int value){
this.value = value;
}
}
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){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public static Node midOrDownMidNode(Node head){
if(head == null || head.next == null || head.next.next == null){
return head;
}
Node slow = head.next;
Node fast = head.next;
while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public static Node midOrUpMidPreNode(Node head){
if(head == null || head.next == null || head.next.next == null){
return head;
}
Node slow = head;
Node fast = head.next.next;
while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public static Node midOrDownMidPreNode(Node head){
if(head == null || head.next == null || head.next.next == null){
return head;
}
Node slow = head;
Node fast = head.next;
while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
2 常见面试题
2.1 判断链表是否是回文结构
给定一个单链表的头节点head,判断该链表是否是回文结构
方法一:使用容器【栈结构】 方法二:改变链表结构【反转链表】
public static class Node{
private Node next;
private int value;
public Node(int value){
this.value = value;
}
}
public static boolean isPalindrome1(Node head){
Stack<Node> stack = new Stack<>();
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;
}
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<>();
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;
}
public static boolean isPalindrome3(Node head){
if(head == null || head.next == null){
return true;
}
Node n1 = head;
Node n2 = head;
while(n2.next != null && n2.next.next != null){
n1 = n1.next;
n2 = n2.next.next;
}
n2 = n1.next;
n1.next = null;
Node n3 = null;
while(n2 != null){
n3 = n2.next;
n2.next = n1;
n1 = n2;
n2 = n3;
}
n3 = n1;
n2 = head;
boolean res = true;
while(n1 != null && n2 != null){
if(n1.value != n2.value){
res = false;
break;
}
n1 = n1.next;
n2 = n2.next;
}
n1 = n3.next;
n3.next = null;
while(n1 != null){
n2 = n1.next;
n1.next = n3;
n3 = n1;
n1 = n2;
}
return res;
}
LeetCode原题:234
代码:
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null){
return true;
}
ListNode n1 = head;
ListNode n2 = head;
while(n2.next != null && n2.next.next != null){
n1 = n1.next;
n2 = n2.next.next;
}
ListNode n3 = n1.next;
n1.next = null;
ListNode next = null;
while(n3 != null){
next = n3.next;
n3.next = n1;
n1 = n3;
n3 = next;
}
n3 = n1;
ListNode save = n1;
n1 = head;
boolean res = true;
while(n1 != null && n3 != null){
if(n1.val != n3.val){
res = false;
break;
}
n1 = n1.next;
n3 = n3.next;
}
n3 = save;
ListNode pre = null;
while(n3 != null){
next = n3.next;
n3.next = pre;
pre = n3;
n3 = next;
}
return res;
}
}
2.2 单向链表划分值【左小、中等、右大】
将单向链表按某值划分成左边小、中间相等、右边大的形式
方法一:将链表放入数组里,在数组上做partition(荷兰国旗问题,快排) 方法二:分成小、中、大三个部分,再把各个部分串起来
2.2.1 使用容器的解法
public static class Node {
private int value;
private Node next;
public Node(int value) {
this.value = value;
}
}
public static Node listPartition1(Node head, int pivot) {
if (head == null) {
return head;
}
Node cur = head;
int i = 0;
while (cur != null) {
i++;
cur = cur.next;
}
Node[] nodeArr = new Node[i];
cur = head;
for (i = 0; i != nodeArr.length; i++) {
nodeArr[i] = cur;
cur = cur.next;
}
arrPartition(nodeArr, pivot);
for (i = 1; i != nodeArr.length; i++) {
nodeArr[i - 1].next = nodeArr[i];
}
nodeArr[i - 1].next = null;
return nodeArr[0];
}
public static void arrPartition(Node[] nodeArr, int pivot) {
int small = -1;
int big = nodeArr.length;
int index = 0;
while (index != big) {
if (nodeArr[index].value < pivot) {
swap(nodeArr, ++small, index++);
} else if (nodeArr[index].value == pivot) {
index++;
} else {
swap(nodeArr, --big, index);
}
}
}
public static void swap(Node[] nodeArr, int a, int b) {
Node temp = nodeArr[a];
nodeArr[a] = nodeArr[b];
nodeArr[b] = temp;
}
2.2.2 不使用容器
public static Node listPartition2(Node head, int pivot) {
Node sH = null;
Node sT = null;
Node eH = null;
Node eT = null;
Node mH = null;
Node mT = null;
Node next = head;
while (head != null) {
next = head.next;
head.next = null;
if (head.value < pivot) {
if (sH == null) {
sH = head;
sT = head;
} else {
sT.next = head;
sT = head;
}
} else if (head.value == pivot) {
if (eH == null) {
eH = head;
eT = head;
} else {
eT.next = head;
eT = head;
}
} else {
if (mH == null) {
mH = head;
mT = head;
} else {
mT.next = head;
mT = head;
}
}
head = next;
}
if(sH != null){
sT.next = eH;
}
if(eH != null){
eT.next = mH;
}
return sH != null ? sH : (eH == null ? mH : eH);
}
2.3 链表复制(next、rand)【LeetCode - 138】
LeetCode原题:https://leetcode.cn/problems/copy-list-with-random-pointer/ 一种特殊的单链表节点如下:
class Node{
private int value;
private Node next;
private Node rand;
public Node(int value){
this.value = value;
}
}
rand指针是单链表节点结构中新增的指针,rand可能指向链表结构中的任意一个节点,也可能指向null 给定一个由Node节点类型组成的无环单链表的头节点head,实现一个函数完成这个链表的复制,并返回新链表的头节点
2.3.1 使用容器map
public static Node copyListWithRandom(Node head){
HashMap<Node, Node> map = new HashMap<>();
Node cur = head;
while(head != null){
map.put(cur, new Node(cur.value));
cur = cur.next;
}
cur = head;
while (cur != null){
map.get(cur).next = map.get(cur.next);
map.get(cur).rand = map.get(cur.rand);
cur = cur.next;
}
return map.get(head);
}
2.3.2 不使用容器
【进阶】: 时间复杂度O(N),额外空间复杂度O(1)
class Solution {
public Node copyRandomList(Node head) {
if(head == null){
return null;
}
Node cur = head;
Node next = null;
while(cur != null){
next = cur.next;
cur.next = new Node(cur.val);
cur.next.next = next;
cur = next;
}
cur = head;
Node copy = null;
while(cur != null){
next = cur.next.next;
copy = cur.next;
copy.random = cur.random == null ? null : cur.random.next;
cur = next;
}
cur = head;
Node res = cur.next;
while(cur != null){
next = cur.next.next;
copy = cur.next;
cur.next = next;
copy.next = next == null ? null : next.next;
cur = next;
}
return res;
}
}
|