pull/6/head
yuanguangxin 5 years ago
parent 594c2795ac
commit d4dc36a4c9

@ -2,8 +2,11 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="af7ffdf2-4ddc-4ed6-8222-60ed5acbc2ed" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/链表操作/q138_复制带随机指针的链表/f1/Node.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/链表操作/q138_复制带随机指针的链表/f1/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/链表操作/q138_复制带随机指针的链表/f2/Node.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/链表操作/q138_复制带随机指针的链表/f2/Solution.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Rocket.md" beforeDir="false" afterPath="$PROJECT_DIR$/Rocket.md" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -66,11 +69,11 @@
</component>
<component name="RecentsManager">
<key name="CopyClassDialog.RECENTS_KEY">
<recent name="链表操作.q138_复制带随机指针的链表.f2" />
<recent name="链表操作.q138_复制带随机指针的链表.f1" />
<recent name="hash相关.q141_环形链表.f2" />
<recent name="hash相关.q141_环形链表.f1" />
<recent name="树的遍历.q102_二叉树的层次遍历" />
<recent name="q110.q2" />
<recent name="q110" />
</key>
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/src/hash相关" />
@ -277,7 +280,7 @@
<workItem from="1582286924766" duration="554000" />
<workItem from="1582552791282" duration="3963000" />
<workItem from="1582634245000" duration="13006000" />
<workItem from="1582873496305" duration="4333000" />
<workItem from="1582873496305" duration="5352000" />
</task>
<task id="LOCAL-00001" summary="add q5/q7/q172">
<created>1580045439607</created>
@ -559,7 +562,14 @@
<option name="project" value="LOCAL" />
<updated>1582723258830</updated>
</task>
<option name="localTasksCounter" value="41" />
<task id="LOCAL-00041" summary="面试题目更新">
<created>1582888928990</created>
<option name="number" value="00041" />
<option name="presentableId" value="LOCAL-00041" />
<option name="project" value="LOCAL" />
<updated>1582888928990</updated>
</task>
<option name="localTasksCounter" value="42" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -612,7 +622,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="add q14/q16/q20" />
<MESSAGE value="add q21" />
<MESSAGE value="add q26/q43" />
<MESSAGE value="add q46" />
@ -637,7 +646,8 @@
<MESSAGE value="增加JVM相关问题" />
<MESSAGE value="add q141/q202/q258" />
<MESSAGE value="更新分类目录" />
<option name="LAST_COMMIT_MESSAGE" value="更新分类目录" />
<MESSAGE value="面试题目更新" />
<option name="LAST_COMMIT_MESSAGE" value="面试题目更新" />
</component>
<component name="WindowStateProjectService">
<state x="320" y="115" key="#Inspections" timestamp="1581061018990">
@ -652,14 +662,14 @@
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="533" y="166" key="#com.intellij.ide.util.MemberChooser/0.0.1440.900@0.0.1440.900" timestamp="1578548165321" />
<state x="303" y="50" key="CommitChangelistDialog2" timestamp="1582723258552">
<state x="303" y="50" key="CommitChangelistDialog2" timestamp="1582900985108">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1582723258552" />
<state x="144" y="78" width="1152" height="720" key="DiffContextDialog" timestamp="1582714686712">
<screen x="0" y="23" width="1440" height="793" />
<state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1582900985108" />
<state x="144" y="78" width="1152" height="720" key="DiffContextDialog" timestamp="1582900981690">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="144" y="78" width="1152" height="720" key="DiffContextDialog/0.0.1440.900@0.0.1440.900" timestamp="1582714675233" />
<state x="144" y="78" width="1152" height="720" key="DiffContextDialog/0.0.1440.900@0.0.1440.900" timestamp="1582900981690" />
<state x="144" y="78" width="1152" height="720" key="DiffContextDialog/0.23.1440.793@0.23.1440.793" timestamp="1582714686712" />
<state x="144" y="78" width="1152" height="720" key="DiffContextDialog/0.23.1440.797@0.23.1440.797" timestamp="1581927787180" />
<state width="1398" height="236" key="GridCell.Tab.0.bottom" timestamp="1582724240264">
@ -706,10 +716,10 @@
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="221" y="63" key="SettingsEditor/0.0.1440.900@0.0.1440.900" timestamp="1581061096545" />
<state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1582723259993">
<state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1582888930786">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1582723259993" />
<state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1582888930786" />
<state x="100" y="100" width="1240" height="700" key="com.intellij.history.integration.ui.views.DirectoryHistoryDialog" timestamp="1581744794182">
<screen x="0" y="23" width="1440" height="797" />
</state>
@ -722,9 +732,9 @@
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="385" y="210" key="run.anything.popup/0.0.1440.900@0.0.1440.900" timestamp="1581652493432" />
<state x="385" y="196" width="670" height="676" key="search.everywhere.popup" timestamp="1580563351587">
<state x="385" y="196" width="670" height="676" key="search.everywhere.popup" timestamp="1582900522166">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="385" y="196" width="670" height="676" key="search.everywhere.popup/0.0.1440.900@0.0.1440.900" timestamp="1580563351587" />
<state x="385" y="196" width="670" height="676" key="search.everywhere.popup/0.0.1440.900@0.0.1440.900" timestamp="1582900522166" />
</component>
</project>

@ -10,6 +10,7 @@
* [q2_两数相加](/src/链表操作/q2_两数相加)
* [q206_反转链表](/src/链表操作/q206_反转链表)
* [q138_复制带随机指针的链表](/src/链表操作/q138_复制带随机指针的链表)
### 双指针遍历/滑动窗口

@ -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;
}
}

@ -0,0 +1,29 @@
package .q138_.f1;
import java.util.HashMap;
/**
* Mapo(n)o(n)
*/
public class Solution {
HashMap<Node, Node> visitedHash = new HashMap<Node, Node>();
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;
}
}

@ -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;
}
}

@ -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;
}
}
Loading…
Cancel
Save