郑州美容网站建设,交通银行网站开发,网络营销推广可以理解为,wordpress可视化函数复制带随机指针的链表 OJ链接 分析#xff1a; 该题的大致题意就是有一个带随机指针的链表#xff0c;复制这个链表但是不能指向原链表的节点#xff0c;所以每一个节点都要复制一遍 大神思路#xff1a; ps:我是学来的 上代码#xff1a;
struct Node* copyRandomList(s… 复制带随机指针的链表 OJ链接 分析 该题的大致题意就是有一个带随机指针的链表复制这个链表但是不能指向原链表的节点所以每一个节点都要复制一遍 大神思路 ps:我是学来的 上代码
struct Node* copyRandomList(struct Node* head)
{//1.在原链表每个节点的后面复制一个节点struct Node* cur head;while(cur){//插入struct Node*copy (struct Node*)malloc(sizeof(struct Node));if(copy NULL){perror(malloc\n);return NULL;}copy-val cur-val;struct Node* next cur-next;cur-next copy;copy-next next;//迭代cur next;} //2.处理randomcur head;while(cur){struct Node*copy cur-next;if(cur-random NULL){copy-random NULL;}else{copy-random cur-random-next;//这个思路的点睛之笔}cur copy-next;//迭代}//3.恢复原链表链接新链表 删除尾插 curhead;struct Node* copyhead NULL;struct Node* copytail NULL;while(cur){struct Node* copy cur-next;struct Node* next copy-next;//用来还原原链表//尾插链接新链表//空链表第一次尾插if(copyhead NULL){copyhead copytail copy;}else{copytail-next copy;//尾插copytail copytail-next;//迭代 }//删除恢复原链表//free(cur-next);//此处不用freecur-next next;cur cur-next;//迭代}return copyhead;
}