Deep clone object graph (simple)
Java
Hard
4 views
Problem Description
Task: clone a linked list node chain without sharing nodes.
Output Format
Return value
Constraints
No cycles assumed.
Official Solution
static class Node{final int val;Node next;Node(int v){val=v;}}static Node cloneChain(Node h){if(h==null) return null;Node nh=new Node(h.val);Node p=nh;for(Node c=h.next;c!=null;c=c.next){p.next=new Node(c.val);p=p.next;}return nh;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!