diff --git a/src/class06/Code03_SmallerEqualBigger.java b/src/class06/Code03_SmallerEqualBigger.java index d234365..a5718ed 100644 --- a/src/class06/Code03_SmallerEqualBigger.java +++ b/src/class06/Code03_SmallerEqualBigger.java @@ -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; } diff --git a/src/class06/Code04_CopyListWithRandom.java b/src/class06/Code04_CopyListWithRandom.java index 5937492..0a0a836 100644 --- a/src/class06/Code04_CopyListWithRandom.java +++ b/src/class06/Code04_CopyListWithRandom.java @@ -15,6 +15,8 @@ public class Code04_CopyListWithRandom { } public static Node copyListWithRand1(Node head) { + // key 老节点 + // value 新节点 HashMap map = new HashMap(); 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; diff --git a/src/class06/Code05_FindFirstIntersectNode.java b/src/class06/Code05_FindFirstIntersectNode.java index 40eb34e..bc27bbb 100644 --- a/src/class06/Code05_FindFirstIntersectNode.java +++ b/src/class06/Code05_FindFirstIntersectNode.java @@ -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 diff --git a/src/class06/Test.java b/src/class06/Test.java index 831e8d6..f833821 100644 --- a/src/class06/Test.java +++ b/src/class06/Test.java @@ -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; - - } - - } diff --git a/src/class07/Code02_UnRecursiveTraversalBT.java b/src/class07/Code02_UnRecursiveTraversalBT.java index ddff515..277e96b 100644 --- a/src/class07/Code02_UnRecursiveTraversalBT.java +++ b/src/class07/Code02_UnRecursiveTraversalBT.java @@ -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 stack = new Stack(); - 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 s2 = new Stack(); 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 + " "); }