zoukankan      html  css  js  c++  java
  • 剑指offer系列——25.复杂链表的复制

    Q:输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
    T:

    A:代码:

        RandomListNode* Clone(RandomListNode* pHead)
        {
            if(!pHead) return NULL;
            RandomListNode *cur = pHead;
            while(cur){
                RandomListNode *node = new RandomListNode(cur->label);
                node->next = cur->next;
                cur->next = node;
                cur = node->next;
            }//直到cur指向了原先链表的结尾null处
            cur = pHead;
            RandomListNode *p;
            while(cur){
                 p = cur->next;
                if(cur->random){
                    p->random = cur->random->next;
                     
                }
                cur = p->next;
            }
             
            RandomListNode *pCloneHead = pHead->next;
            RandomListNode *tmp;
            cur = pHead;
            while(cur->next){
                tmp = cur->next;
                cur->next =tmp->next;
                cur = tmp;
            }
            return pCloneHead;
        }
    
  • 相关阅读:
    Azure SQL Storage
    T-SQL quries
    映射盘符
    繁体及其输入法、乱码问题
    匈牙利命名法
    C++四种转换总结
    windows系统下进程间通信
    Qt 中文字符串问题
    PDB文件详解
    DbgView 无法开启Capture Kernel问题
  • 原文地址:https://www.cnblogs.com/xym4869/p/12291500.html
Copyright © 2011-2022 走看看