哪些网站可以做引流,建设公司网站的可行性研究,做网站排名,网站快速刷排名工具题目一#xff1a; 题目链接#xff1a;
思路一#xff1a; 找相对位置暴力求解的方法#xff1a; 1.复制一个新的链表出来遍历老的节点给新的节点赋值#xff0c;random这个时候不去值。 2.两个链表同时遍历#xff0c;遍历老链表的时候去寻找相对位置#xff0c;在遍…题目一 题目链接
思路一 找相对位置暴力求解的方法 1.复制一个新的链表出来遍历老的节点给新的节点赋值random这个时候不去值。 2.两个链表同时遍历遍历老链表的时候去寻找相对位置在遍历新的链表找到随机值赋值。 struct Node* copyRandomList(struct Node* head) {struct Node* curhead;struct Node* newheadNULL,*tileNULL;//复制原来的链表数据while(cur){//开辟新的节点struct Node* newnode(struct Node*)malloc(sizeof(struct Node));newnode-valcur-val;newnode-nextNULL;newnode-randomNULL;if(newheadNULL){tilenewheadnewnode;}else{tile-nextnewnode;tiletile-next;}curcur-next;}//进行两个的循环遍历找相对位置curhead;struct Node* cur2newhead;int pos0;while(cur){//更新一下pospos0;//cur的随机值是哪一个struct Node* findcur-random;if(findNULL){cur2-randomNULL;curcur-next;cur2cur2-next;continue;}else{struct Node* curoldhead;while(curold){if(findcurold){break;}pos;curoldcurold-next;}}//寻找随机节点struct Node* curnewnewhead;while(pos){curnewcurnew-next;pos--;}cur2-randomcurnew;//循环条件curcur-next;cur2cur2-next;}return newhead;
}思路二: struct Node* copyRandomList(struct Node* head) {struct Node* cur head, * tile NULL;//新的链表赋值插入,cur为空才结束插入while (cur){//保存下一个老的tile cur-next;struct Node* newnode (struct Node*)malloc(sizeof(struct Node));newnode-val cur-val;cur-next newnode;newnode-next tile;//循环条件cur tile;}//给copy链表赋值randomstruct Node* copy NULL;cur head;tile NULL;while (cur){//连接了新的节点copy cur-next;tile copy-next;//给random赋值随机值正常值的两个情况if (cur-random NULL){copy-random NULL;}else{copy-random cur-random-next;}//循环的移动cur tile;}copy NULL;cur head;tile NULL;//分离链表struct Node* newhead NULL;struct Node* move NULL;while (cur){copy cur-next;tile copy-next;if (newhead NULL){newhead copy;move newhead;}else{move-next copy;move move-next;}//恢复原来的节点cur-next tile;//循环遍历cur tile;}return newhead;}