From d4dc36a4c9425ac41c48fcb96e7384d742df1a50 Mon Sep 17 00:00:00 2001
From: yuanguangxin <274841922@qq.com>
Date: Fri, 28 Feb 2020 22:43:23 +0800
Subject: [PATCH] add q138
---
.idea/workspace.xml | 42 ++++++++++++-------
README.md | 1 +
.../f1/Node.java | 13 ++++++
.../f1/Solution.java | 29 +++++++++++++
.../f2/Node.java | 13 ++++++
.../f2/Solution.java | 39 +++++++++++++++++
6 files changed, 121 insertions(+), 16 deletions(-)
create mode 100644 src/链表操作/q138_复制带随机指针的链表/f1/Node.java
create mode 100644 src/链表操作/q138_复制带随机指针的链表/f1/Solution.java
create mode 100644 src/链表操作/q138_复制带随机指针的链表/f2/Node.java
create mode 100644 src/链表操作/q138_复制带随机指针的链表/f2/Solution.java
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index d0de09c..9b0c692 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -2,8 +2,11 @@
+
+
+
+
-
@@ -66,11 +69,11 @@
+
+
-
-
@@ -277,7 +280,7 @@
-
+
1580045439607
@@ -559,7 +562,14 @@
1582723258830
-
+
+ 1582888928990
+
+
+
+ 1582888928990
+
+
@@ -612,7 +622,6 @@
-
@@ -637,7 +646,8 @@
-
+
+
@@ -652,14 +662,14 @@
-
+
-
-
-
+
+
+
-
+
@@ -706,10 +716,10 @@
-
+
-
+
@@ -722,9 +732,9 @@
-
+
-
+
\ No newline at end of file
diff --git a/README.md b/README.md
index 0e0b3ba..a90dfaf 100644
--- a/README.md
+++ b/README.md
@@ -10,6 +10,7 @@
* [q2_两数相加](/src/链表操作/q2_两数相加)
* [q206_反转链表](/src/链表操作/q206_反转链表)
+* [q138_复制带随机指针的链表](/src/链表操作/q138_复制带随机指针的链表)
### 双指针遍历/滑动窗口
diff --git a/src/链表操作/q138_复制带随机指针的链表/f1/Node.java b/src/链表操作/q138_复制带随机指针的链表/f1/Node.java
new file mode 100644
index 0000000..b0d4b1b
--- /dev/null
+++ b/src/链表操作/q138_复制带随机指针的链表/f1/Node.java
@@ -0,0 +1,13 @@
+package 链表操作.q138_复制带随机指针的链表.f1;
+
+class Node {
+ int val;
+ Node next;
+ Node random;
+
+ public Node(int val) {
+ this.val = val;
+ this.next = null;
+ this.random = null;
+ }
+}
diff --git a/src/链表操作/q138_复制带随机指针的链表/f1/Solution.java b/src/链表操作/q138_复制带随机指针的链表/f1/Solution.java
new file mode 100644
index 0000000..00e7f6e
--- /dev/null
+++ b/src/链表操作/q138_复制带随机指针的链表/f1/Solution.java
@@ -0,0 +1,29 @@
+package 链表操作.q138_复制带随机指针的链表.f1;
+
+import java.util.HashMap;
+
+/**
+ * 用Map存储遍历过的节点,时间o(n),额外空间o(n)
+ */
+public class Solution {
+
+ HashMap visitedHash = new HashMap();
+
+ public Node copyRandomList(Node head) {
+
+ if (head == null) {
+ return null;
+ }
+ if (this.visitedHash.containsKey(head)) {
+ return this.visitedHash.get(head);
+ }
+
+ Node node = new Node(head.val);
+
+ this.visitedHash.put(head, node);
+ node.next = this.copyRandomList(head.next);
+ node.random = this.copyRandomList(head.random);
+
+ return node;
+ }
+}
\ No newline at end of file
diff --git a/src/链表操作/q138_复制带随机指针的链表/f2/Node.java b/src/链表操作/q138_复制带随机指针的链表/f2/Node.java
new file mode 100644
index 0000000..8dd6ddc
--- /dev/null
+++ b/src/链表操作/q138_复制带随机指针的链表/f2/Node.java
@@ -0,0 +1,13 @@
+package 链表操作.q138_复制带随机指针的链表.f2;
+
+class Node {
+ int val;
+ Node next;
+ Node random;
+
+ public Node(int val) {
+ this.val = val;
+ this.next = null;
+ this.random = null;
+ }
+}
diff --git a/src/链表操作/q138_复制带随机指针的链表/f2/Solution.java b/src/链表操作/q138_复制带随机指针的链表/f2/Solution.java
new file mode 100644
index 0000000..9bc68da
--- /dev/null
+++ b/src/链表操作/q138_复制带随机指针的链表/f2/Solution.java
@@ -0,0 +1,39 @@
+package 链表操作.q138_复制带随机指针的链表.f2;
+
+/**
+ * 在每一个链表的节点后都新连一个节点之后操作 时间o(n) 额外空间o(1)
+ */
+public class Solution {
+ public Node copyRandomList(Node head) {
+
+ if (head == null) {
+ return null;
+ }
+
+ Node ptr = head;
+ while (ptr != null) {
+ Node newNode = new Node(ptr.val);
+ newNode.next = ptr.next;
+ ptr.next = newNode;
+ ptr = newNode.next;
+ }
+
+ ptr = head;
+
+ while (ptr != null) {
+ ptr.next.random = (ptr.random != null) ? ptr.random.next : null;
+ ptr = ptr.next.next;
+ }
+
+ Node ptrOldList = head;
+ Node ptrNewList = head.next;
+ Node headOld = head.next;
+ while (ptrOldList != null) {
+ ptrOldList.next = ptrOldList.next.next;
+ ptrNewList.next = (ptrNewList.next != null) ? ptrNewList.next.next : null;
+ ptrOldList = ptrOldList.next;
+ ptrNewList = ptrNewList.next;
+ }
+ return headOld;
+ }
+}