modify code on class

pull/6/head
左程云 4 years ago
parent 674086e1df
commit b579372ed7

@ -101,8 +101,10 @@ public class Code03_SmallerEqualBigger {
sT.next = eH;
eT = eT == null ? sT : eT; // 下一步谁去连大于区域的头谁就变成eT
}
// 上面的if不管跑了没有et
// all reconnect
// 下一步一定是需要用eT 去接 大于区域的头
// 有等于区域eT -> 等于区域的尾结点
// 无等于区域eT -> 小于区域的尾结点
// eT 尽量不为空的尾巴节点
if (eT != null) { // 如果小于区域和等于区域,不是都没有
eT.next = mH;
}

@ -15,6 +15,8 @@ public class Code04_CopyListWithRandom {
}
public static Node copyListWithRand1(Node head) {
// key 老节点
// value 新节点
HashMap<Node, Node> map = new HashMap<Node, Node>();
Node cur = head;
while (cur != null) {
@ -25,6 +27,7 @@ public class Code04_CopyListWithRandom {
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;

@ -32,21 +32,22 @@ public class Code05_FindFirstIntersectNode {
return null;
}
// n1 慢 n2 快
Node n1 = head.next; // n1 -> slow
Node n2 = head.next.next; // n2 -> fast
while (n1 != n2) {
if (n2.next == null || n2.next.next == null) {
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;
}
n2 = n2.next.next;
n1 = n1.next;
fast = fast.next.next;
slow = slow.next;
}
n2 = head; // n2 -> walk again from head
while (n1 != n2) {
n1 = n1.next;
n2 = n2.next;
// slow fast 相遇
fast = head; // n2 -> walk again from head
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return n1;
return slow;
}
// 如果两个链表都无环返回第一个相交节点如果不想交返回null

@ -2,25 +2,4 @@ package class06;
public class Test {
public static class Node{
public int value;
public Node next;
public Node(int v) {
value = v;
}
}
public static void main(String[] args) {
Node a = new Node(1);
Node b = new Node(2);
Node c = new Node(3);
a.next = b;
b.next = c;
c = null;
}
}

@ -33,18 +33,18 @@ public class Code02_UnRecursiveTraversalBT {
System.out.println();
}
public static void in(Node head) {
public static void in(Node cur) {
System.out.print("in-order: ");
if (head != null) {
if (cur != null) {
Stack<Node> stack = new Stack<Node>();
while (!stack.isEmpty() || head != null) {
if (head != null) {
stack.push(head);
head = head.left;
while (!stack.isEmpty() || cur != null) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
head = stack.pop();
System.out.print(head.value + " ");
head = head.right;
cur = stack.pop();
System.out.print(cur.value + " ");
cur = cur.right;
}
}
}
@ -58,7 +58,7 @@ public class Code02_UnRecursiveTraversalBT {
Stack<Node> s2 = new Stack<Node>();
s1.push(head);
while (!s1.isEmpty()) {
head = s1.pop();
head = s1.pop(); // 头 右 左
s2.push(head);
if (head.left != null) {
s1.push(head.left);
@ -67,6 +67,7 @@ public class Code02_UnRecursiveTraversalBT {
s1.push(head.right);
}
}
// 左 右 头
while (!s2.isEmpty()) {
System.out.print(s2.pop().value + " ");
}

Loading…
Cancel
Save