当前位置: 首页 > 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/25854.html

相关文章:

  • wordpress自定义排序搜索引擎优化seo信息
  • 西安北郊网站开发佛山seo培训
  • 网站建设用什么软件厨师培训
  • 江苏建设工程信息网官网入口重庆旅游seo整站优化
  • 现在建设一个网站需要什么技术凡科网
  • 公安网站备案一般多长时间高清视频线转换线
  • 教做面食的网站百度sem代运营
  • 企业邮箱登录入口126搜索引擎优化实训心得
  • wordpress网站嵌入商城一个新产品怎么推广
  • 海安建设局网站百度app打开
  • 哪个网站可以接任务做兼职个人博客网页设计
  • 做php网站教程河北seo人员
  • 制作网站书签怎么做发外链比较好的平台
  • 网站开发论文的分类号站长工具百度
  • 辞职做网站icp备案查询
  • 网站建设介绍文字百度app安装下载免费
  • 成都网站建设 erp360广告投放平台
  • 手机网页自动跳转怎么处理seo搜索引擎优化怎么优化
  • 网站构建代码模板万能bt搜索引擎
  • 驾校网站建设费用网络营销的十种方法
  • 网站优化推广怎么做磁力猫torrentkitty官网
  • 学校官网在哪里找手机优化软件哪个好
  • 有没有什么做统计的网站网站的收录情况怎么查
  • 西安品牌网站建设服务商站长工具seo综合查询分析
  • 怎么做网站识图验证码周口网络推广哪家好
  • 发布工程信息的网站有哪些百度网址大全下载
  • 长春网站关键词排名长春seo优化企业网络跃升
  • 专业建设费用怎么花爱站网seo查询
  • vs和sql做购物网站广告门
  • 做阿里网站卖东西赚钱广告推广渠道