剑指offer:复杂链表的复制中遇到的问题
各位大佬,这是剑指offer:复杂链表的复制,为什么我的提交
对应输出应该为:
{1,2,3,4,5,3,5,#,2,#}
你的输出为:
{1,2,3,4,5,#,#,#,#,#}。
{1,2,3,4,5,3,5,#,2,#}
你的输出为:
{1,2,3,4,5,#,#,#,#,#}。
,哪里出错了?这是源码:
/* public class RandomListNode { int label; RandomListNode next = null; RandomListNode random = null; RandomListNode(int label) { this.label = label; } } */ public class Solution { public RandomListNode Clone(RandomListNode pHead) { CloneNodes(pHead); ConnectRandomNodes(pHead); return ReconnectNodes(pHead); } void CloneNodes(RandomListNode pHead) { RandomListNode tmp=pHead; while (tmp!=null) { RandomListNode clonedNode=new RandomListNode(0); clonedNode.label=tmp.label; clonedNode.next=tmp.next; clonedNode.random=null; tmp.next=clonedNode; tmp=clonedNode.next; } } void ConnectRandomNodes(RandomListNode pHead) { RandomListNode tmp=pHead; while (tmp!=null) { RandomListNode clonedNode=tmp.next; if (clonedNode.random!=null){ clonedNode.random=tmp.random.next; } tmp=clonedNode.next; } } RandomListNode ReconnectNodes(RandomListNode pHead) { RandomListNode tmp=pHead; RandomListNode clonedHead=null; RandomListNode clonedNode=null; if (tmp!=null) { clonedHead=clonedNode=tmp.next; tmp.next=clonedNode.next; tmp=tmp.next; } while (tmp!=null) { clonedNode.next=tmp.next; clonedNode=clonedNode.next; tmp.next=clonedNode.next; tmp=tmp.next; } return clonedHead; } }
#笔试题目#