Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

剑指offer23题发现的问题 #26

Open
Qinlong275 opened this issue Mar 2, 2018 · 0 comments
Open

剑指offer23题发现的问题 #26

Qinlong275 opened this issue Mar 2, 2018 · 0 comments

Comments

@Qinlong275
Copy link

剑指offer23题这里,三个函数传入的是同一个head,这样有问题吧,cloneNodes(head);执行完,head就不是头节点了,再去执行 connectNodes(head);就错了。应给给一个临时变量cur,每个方法执行前赋值为head, 然后方法调用传入cur.
/**
* 实现函复制一个复杂链表。在复杂链表中,每个结点除了有一个next字段指向下一个结点外,
* 还有一个sibling字段指向链表中的任意结点或者NULL
*
* @param head 链表表头结点
* @return 复制结点的头结点
*/
public static ComplexListNode clone(ComplexListNode head) {
// 如果链表为空就直接返回空
if (head == null) {
return null;
}

    // 先复制结点
    cloneNodes(head);
    // 再链接sibling字段
    connectNodes(head);
    // 将整个链表拆分,返回复制链表的头结点
    return reconnectNodes(head);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant