当前位置: 首页 > news >正文

化工行业网站免费新闻源发布平台

化工行业网站,免费新闻源发布平台,想学做蛋糕用哪一个网站,微信网站建设报价表方法一:暴力求解 1️⃣遍历原链表,复制节点尾插 2️⃣更新random,原链表中的random对应第几个节点则复制链表中的random就对应第几个 📖Note 不能通过节点中的val判断random的指向,因为链表中可能存在两个val相等的节点…

方法一:暴力求解

1️⃣遍历原链表,复制节点尾插

2️⃣更新random,原链表中的random对应第几个节点则复制链表中的random就对应第几个

📖Note

不能通过节点中的val判断random的指向,因为链表中可能存在两个val相等的节点

//创建节点
struct Node* BuyNode(int x)
{struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));newnode->val = x;newnode->next = NULL;return newnode;
}//找到random对应的节点是第几个
int FindRandom(struct Node* head, struct Node* random)
{int count = 1;while (head){if (head == random){return count;}else {count++;head = head->next;}}return count;
}struct Node* copyRandomList(struct Node* head) {struct Node* guard = (struct Node*)malloc(sizeof(struct Node));guard->next = NULL;struct Node* tail = guard;struct Node* cur = head;//复制原链表while (cur){struct Node* newnode = BuyNode(cur->val);tail->next = newnode;tail = tail->next;cur = cur->next;}//tail和cur都指向新链表的头tail = guard->next;struct Node* tmp = head;//更新randomwhile (tail){//在原链表这种判断random指向的节点是第几个int count = FindRandom(head, tmp->random);tmp = tmp->next;//更新复制链表中的randomcur = guard->next;while (count--){tail->random = cur;if (cur){cur = cur->next;}}tail = tail->next;}struct Node* newhead = guard->next;free(guard);return newhead;}

方法二:

1️⃣拷贝原节点,并链接在原节点之后

2️⃣更新拷贝节点中的random

拷贝节点中的random指向的是原节点中random指向节点的下一个节点

3️⃣将拷贝的节点解下来构成新的复制链表

struct Node* copyRandomList(struct Node* head) {struct Node* cur = head;struct Node* copy = NULL;//拷贝原节点,并链接在原节点之后while (cur){copy = (struct Node*)malloc(sizeof(struct Node));copy->val = cur->val;copy->next = cur->next;cur->next = copy;cur = cur->next->next;}//更新拷贝节点的randomcur = head;while (cur){copy = cur->next;if (cur->random){copy->random = cur->random->next;}else{copy->random = NULL;}cur = cur->next->next;}//将所有拷贝节点解下来构成新链表并恢复原链表结构cur = head;struct Node* copyhead, *copytail;copyhead = copytail = NULL;while (cur){copy = cur->next;//取节点尾插if (copytail == NULL){copyhead = copytail = copy;}else{copytail->next = copy;copytail = copytail->next;}//恢复原链表cur->next = copy->next;cur = copy->next;}return copyhead;
}

http://www.tj-hxxt.cn/news/42673.html

相关文章:

  • 网站运营费用预算谁有恶意点击软件
  • 梧州网站设计谷歌浏览器下载app
  • 铺面怎样做放上网站不受国内限制的浏览器
  • 怎么将公司网站设成首页如何设计网站步骤
  • 网站开发知识体系网络推广技巧
  • 模具公司网站中品质见证怎么做制作网站要多少费用
  • 企业建网站品牌怎样自己开发一款软件
  • wordpress order插件兰州seo新站优化招商
  • html5手机网站开发教程百度指数特点
  • 重庆做网站哪个好些嘛百度免费下载安装百度
  • wordpress博客添加到菜单兰州网站seo
  • 口碑好的番禺网站建设app运营需要做哪些
  • 网站功能配置网站搜索优化找哪家
  • 织梦网站建设教程恶意点击广告软件
  • 皮革城网站建设方案重庆百度
  • 今日上海新闻最新消息seo外链怎么发
  • 手机做任务的网站有哪些seo 优化思路
  • 武汉网上商城网站建设上海搜索引擎推广公司
  • 官方网站下载6966seo优化与推广招聘
  • 网站留言程序怎么做没有限制的国外搜索引擎
  • 中牟网站建设seo关键词优化软件app
  • 做一个英文网站多少钱推广方式有哪些?
  • 济南建设网站上海百度整站优化服务
  • 网站建设网站需要什么2023网站分享
  • 网网站开发设计百度快照怎么看
  • 深圳网站建设公司元网站制作模板
  • 德阳北京网站建设上海网络推广需要多少
  • 东莞凤岗网站建设制作如何写软文推广产品
  • wordpress图片上传西安网络seo公司
  • 彩神app官方网站开发关键词优化系统