ublic class IsPalindromList {
public static class Node {
public int val;
public Node next;
public Node(int data) {
val = data;
next = null;
}
}
public static boolean isPalindrom1(Node head) {
if (head == null || head.next == null) {
return true;
}
Stack<Node> stack = new Stack<>();
Node mid = head;
Node fast = head;
while (fast.next != null && fast.next.next != null) {
mid = mid.next;
fast = fast.next.next;
}
while (mid.next != null) {
mid = mid.next;
stack.add(mid);
}
Node i = head;
boolean ans = true;
while (stack.size() != 0) {
if (i.val != stack.pop().val) {
ans = false;
break;
}
i = i.next;
}
return ans;
}
public static boolean isPalindrom2(Node head) {
if (head == null || head.next == null) {
return true;
}
// 找到中间节点
Node mid = head;
Node fast = head;
while (fast.next != null && fast.next.next != null) {
mid = mid.next;
fast = fast.next.next;
}
Node cur = mid;
// 后边段链表反转
Node pre = null;
Node next = null;
while (cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
Node left = head;
Node right = pre;
boolean ans = true;
while (left != null && right != null) {
if (left.val != right.val) {
ans = false;
break;
}
left = left.next;
right = right.next;
}
cur = pre;
pre = null;
next = null;
while (cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return ans;
}
public static void main(String[] args) {
Node head = new Node(0);
head.next = new Node(1);
head.next.next = new Node(2);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);
head.next.next.next.next.next = new Node(0);
System.out.println(isPalindrom2(head));
System.out.println(isPalindrom1(head));
}
}
链表的荷兰国旗问题,给定一个链表头节点,一个划分值
(其实我第一次写的时候第二种方法比第一种方法写的快,因为思路简单,不绕。)
partition
过程public class SmallEqualBig {
public static class Node {
public int val;
public Node next;
public Node(int val) {
this.val = val;
next = null;
}
}
public static Node listPartition1(Node head, int pivot) {
if (head == null || head.next == null) {
return head;
}
int index = 1;
Node cur = head;
while (cur.next != null) {
index++;
cur = cur.next;
}
Node[] arr = new Node[index];
cur = head;
for (int i = 0; i < arr.length; i++) {
arr[i] = cur;
cur = cur.next;
}
partition(arr,pivot);
for (index = 1; index != arr.length; index++) {
arr[index - 1].next = arr[index];
}
arr[index - 1].next = null;
return arr[0];
}
public static Node listPartition2(Node head, int pivot) {
if (head == null || head.next == null) {
return head;
}
Node smallHead = null;
Node smallTail = null;
Node equalHead = null;
Node equalTail = null;
Node bigHead = null;
Node bigTail = null;
Node next = null;
while (head != null) {
next = head.next;
head.next = null;
if (head.val < pivot) {
if (smallHead == null) {
smallHead = head;
smallTail = head;
}else {
smallTail.next = head;
smallTail = smallTail.next;
}
} else if (head.val == pivot) {
if (equalHead == null) {
equalHead = head;
equalTail = head;
}else {
equalTail.next = head;
equalTail = equalTail.next;
}
}else {
if (bigHead == null) {
bigHead = head;
bigTail = head;
}else {
bigTail.next = head;
bigTail = bigTail.next;
}
}
head = next;
}
if (smallTail != null) {
smallTail.next = equalHead;
equalTail = equalTail == null ? smallTail : equalTail;
}
if (equalTail != null) {
equalTail.next = bigHead;
}
return smallHead == null ? (equalHead == null ? bigHead : equalHead) : smallHead;
}
private static void partition(Node[] arr, int pivot) {
int small = -1;
int big = arr.length;
int index = 0;
while (index < big) {
if(arr[index].val < pivot) {
swap(arr, index++, ++small);
} else if (arr[index].val > pivot) {
swap(arr, index++, --big);
}else {
index++;
}
}
}
private static void swap(Node[] arr, int i, int j) {
Node temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
Node head = new Node(9);
head.next = new Node(1);
head.next.next = new Node(2);
head.next.next.next = new Node(6);
head.next.next.next.next = new Node(4);
head.next.next.next.next.next = new Node(5);
head.next.next.next.next.next.next = new Node(2);
Node node = listPartition1(head, 2);
while (node != null) {
System.out.print(node.val + " ");
node = node.next;
}
System.out.println();
Node head2 = new Node(9);
head2.next = new Node(1);
head2.next.next = new Node(2);
head2.next.next.next = new Node(6);
head2.next.next.next.next = new Node(4);
head2.next.next.next.next.next = new Node(5);
head2.next.next.next.next.next.next = new Node(2);
Node node1 = listPartition2(head2, 2);
while (node1 != null) {
System.out.print(node1.val + " ");
node1 = node1.next;
}
}
}
复制特殊链表,单链表中加了个rand指针,可能指向任意一个节点,也可能指向null
给定这个链表的头节点,完成链表的复制
返回复制的新链表的头节点。时间复杂度O(N),额外空间复杂度O(1)
方法一(笔试):
方式二(面试):
public class CopyListWithRandom {
public static class Node {
public int val;
public Node next;
public Node rand;
public Node(int val) {
this.val = val;
}
}
// map方法
public static Node copyListWithRandom(Node head) {
if (head == null) {
return null;
}
HashMap<Node, Node> map = new HashMap<>();
Node cur = head;
while (cur != null) {
map.put(cur, new Node(cur.val));
cur = cur.next;
}
Node ans = map.get(head);
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 ans;
}
// 不借助容器
public static Node copyListWithRandom2(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;
}
Node copyNode = null;
cur = head;
while (cur != null) {
copyNode = cur.next;
copyNode.rand = cur.rand == null ? null : cur.rand.next;
cur = copyNode.next;
}
Node ans = head.next;
cur = head;
while (cur != null) {
next = cur.next.next;
copyNode = cur.next;
copyNode.next = next == null ? null : next.next;
cur.next = next;
cur = next;
}
return ans;
}
public static void main(String[] args) {
Node head = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
head.next = node2;
node2.next = node3;
head.rand = node3;
node2.rand = head;
Node node = copyListWithRandom(head);
while (node != null) {
System.out.println(node.val + " ");
System.out.println(node == head);
node = node.next;
head = head.next;
}
}
}
给定两个可能有环也可能无环的单链表,给定头节点1和头节点2.
请实现一个函数,如果两个链表相交,请返回相交的第一个节点,如果不相交,返回null
时间复杂度O(N),额外空间复杂度O(1)
public class FindFirstIntersectNode {
public static class Node {
public int val;
public Node next;
public Node(int val) {
this.val = val;
}
}
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;
}
private static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
Node cur1 = head1;
Node cur2 = head2;
if (loop1 == loop2) {
int n = 0;
while (cur1.next != null) {
n++;
cur1 = cur1.next;
}
while (cur2.next != null) {
n--;
cur2 = cur2.next;
}
cur1 = n > 0 ? head1 : head2;
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 loop2;
}
cur1 = cur1.next;
}
return null;
}
}
private static Node noLoop(Node head1, Node head2) {
Node cur1 = head1;
Node cur2 = head2;
int n = 0;
while (cur1.next != null) {
n++;
cur1 = cur2.next;
}
while (cur2.next != null) {
n--;
cur2 = cur2.next;
}
if (cur1 != cur2) {
return null;
}
cur1 = n < 0 ? head2 : head1;
cur2 = cur1 == head2 ? head1 : head2;
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
private static Node getLoopNode(Node head) {
if (head.next == null || head.next.next ==null) {
return null;
}
Node slow = head.next;
Node fast = head.next.next;
while (fast != slow) {
if (fast == null) {
return null;
}
slow = slow.next;
fast = fast.next.next;
}
fast = head;
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
public static void main(String[] args) {
Node head = new Node(0);
head.next = new Node(1);
head.next.next = new Node(2);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(4);
head.next.next.next.next.next = head.next.next;
Node head1 = new Node(0);
head1.next = new Node(1);
head1.next.next = new Node(2);
head1.next.next.next = new Node(3);
head1.next.next.next.next = new Node(4);
head1.next.next.next.next.next = head.next.next.next;
Node intersectNode = getIntersectNode(head, head1);
System.out.println(intersectNode.val);
}
}