133. Clone Graph && 138. Copy List with Random Pointer
133. Clone Graph
Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.
OJ's undirected graph serialization:
Nodes are labeled uniquely.
We use #
as a separator for each node, and ,
as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}
.
The graph has a total of three nodes, and therefore contains three parts as separated by #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1 / \ / \ 0 --- 2 / \ \_/
Recursion solution:
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */ public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if(node == null) return null; Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); return cloneGraph(node, map); } private UndirectedGraphNode cloneGraph(UndirectedGraphNode node, Map<UndirectedGraphNode, UndirectedGraphNode> map) { UndirectedGraphNode nodeCopy = new UndirectedGraphNode(node.label); map.put(node, nodeCopy); for(UndirectedGraphNode neighbor: node.neighbors) { UndirectedGraphNode copy = map.get(neighbor); if(copy == null) { copy = cloneGraph(neighbor, map); } nodeCopy.neighbors.add(copy); } return nodeCopy; } }
Iterative solution:
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */ public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if(node == null) return null; Map<UndirectedGraphNode, UndirectedGraphNode> copiesDict = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); UndirectedGraphNode nodeCopy = new UndirectedGraphNode(node.label); copiesDict.put(node,nodeCopy); Queue<UndirectedGraphNode> q = new ArrayDeque<UndirectedGraphNode>(); q.add(node); while(!q.isEmpty()) { UndirectedGraphNode n = q.remove(); UndirectedGraphNode nCopy = copiesDict.get(n); for(UndirectedGraphNode nodeToVisit: n.neighbors) { UndirectedGraphNode copy = copiesDict.get(nodeToVisit); if(copy == null) { copy = new UndirectedGraphNode(nodeToVisit.label); copiesDict.put(nodeToVisit, copy); q.add(nodeToVisit); } nCopy.neighbors.add(copy); } } return nodeCopy; } }
138. Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.
/** * Definition for singly-linked list with a random pointer. * class RandomListNode { * int label; * RandomListNode next, random; * RandomListNode(int x) { this.label = x; } * }; */ public class Solution { public RandomListNode copyRandomList(RandomListNode head) { if(head == null) return null; Map<RandomListNode, RandomListNode> m = new HashMap<RandomListNode, RandomListNode>(); return copyRandomList(m, head); } private RandomListNode copyRandomList(Map<RandomListNode, RandomListNode> map, RandomListNode node) { RandomListNode copy = new RandomListNode(node.label); map.put(node, copy); if(node.next != null){ RandomListNode nextCopy = map.get(node.next); if(nextCopy == null){ nextCopy = copyRandomList(map, node.next); } copy.next = nextCopy; } if(node.random != null){ RandomListNode randomCopy = map.get(node.random); if(randomCopy == null){ randomCopy = copyRandomList(map, node.random); } copy.random = randomCopy; } return copy; } }