做网站应该怎么做,鞍山百姓网免费发布信息,wordpress zhuce邮件,郑州建设银行网点地址查询Practice makes perfect#xff01; 实战一#xff1a; 思路#xff1a;我们要用复制的节点来组成一个新的链表#xff0c;而原链表的节点随机指向其中一个节点#xff0c;我们首先给每一个节点都复制并且插入到原来节点的后面#xff0c;然后用复制的节点指向我们原来节… Practice makes perfect 实战一 思路我们要用复制的节点来组成一个新的链表而原链表的节点随机指向其中一个节点我们首先给每一个节点都复制并且插入到原来节点的后面然后用复制的节点指向我们原来节点指向的节点最后在将复制的节点拿下来尾插到新的链表。 struct Node* copyRandomList(struct Node* head) {struct Node* curhead;while(cur){struct Node* copy(struct Node*)malloc(sizeof(struct Node));copy-valcur-val;copy-nextcur-next;cur-nextcopy;curcur-next-next;}curhead;while(cur){struct Node* copycur-next; if(cur-randomNULL){copy-randomNULL;}else{copy-randomcur-random-next;}curcur-next-next;}struct Node* newheadNULL;struct Node* tailNULL;curhead;while(cur){struct Node* copycur-next;struct Node* nextcopy-next;if(tailNULL){newheadtailcopy;}else{tail-nextcopy;tailtail-next;}cur-nextnext;curnext;}return newhead;}实战二 思路这里我们有两个链表我们将两个链表中小的插到前面形成一个升序的新链表那么我们首先就要考虑两个链表是否为空如果链表一为空那么我们的新链表就是链表二那么链表二为空新链表就是链表一如果都不为空我们就要比较两个两个链表节点的大小了小的节点放在前面如果链表一比链表二小那么链表一的节点就放在前面反之则链表二的节点放在前面如果一个链表为空了那么我们只要将剩下的那个不为空的链表尾插到新链表的后面就可以了。 struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {if(list1NULL){return list2;}if(list2NULL){return list1;}struct ListNode* headNULL;struct ListNode* tailNULL;while(list1list2){if(list1-vallist2-val){if(tailNULL){headtaillist1;}else{tail-nextlist1;tailtail-next;}list1list1-next;}else{if(tailNULL){headtaillist2;}else{tail-nextlist2;tailtail-next;}list2list2-next;}}if(list1){tail-nextlist1;}if(list2){tail-nextlist2;}return head;}继续加油我们下期见