Java dfs Solution

  • 1

    post a recursion solution using DFS

    public class Solution {
        public RandomListNode copyRandomList(RandomListNode head) {
        	Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
        	return dfs(head, map);
    	private RandomListNode dfs(RandomListNode head, Map<RandomListNode, RandomListNode> map) {
    		// TODO Auto-generated method stub
    		if(head == null) return null;
    		if(map.containsKey(head)) return map.get(head);
    		RandomListNode copy = new RandomListNode(head.label);
    		map.put(head, copy); = dfs(, map);
    		copy.random = dfs(head.random, map);
    		return copy;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.