复杂链表的赋值
来源:互联网 发布:2010年总决赛数据 编辑:程序博客网 时间:2024/06/05 10:27
链表结构中,每个节点不仅有一条指向下一节点的next指针,同时含有一条rand指针,rand指针可能指向任何一个链表中的节点,请复制这种含有rand指针节点的链表。
方法1.创建一个没有rand时与复杂链表完全相同的链表,然后遍历原链表,给新链表rand复制。
方法2.
public class Solution {public RandomListNode Clone(RandomListNode head) {if (head == null) {return null;}RandomListNode cur = head;RandomListNode next = null;// copy node and link to every nodewhile (cur != null) {next = cur.next;cur.next = new RandomListNode(cur.label);cur.next.next = next;cur = next;}cur = head;RandomListNode curCopy = null;// set copy node randwhile (cur != null) {next = cur.next.next;curCopy = cur.next;curCopy.random = cur.random != null ? cur.random.next : null;cur = next;}RandomListNode res = head.next;cur = head;// splitwhile (cur != null) {next = cur.next.next;curCopy = cur.next;cur.next = next;curCopy.next = next != null ? next.next : null;cur = next;}return res;}}
阅读全文
0 0
- 复杂链表的赋值
- 复杂链表的赋值
- 剑指offer--复杂链表的赋值
- 面试题26:复杂链表的赋值
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- Linux常用命令(持续更新中)
- Android Studio 运行出现 Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug
- AngularJS学习之Link & Complie (一)
- git使用笔记(创建和合并分支)
- 基于libyuv库的NV12格式图片的缩放
- 复杂链表的赋值
- iOS10.3新特性图文介绍
- hdu n皇后问题 dfs打表
- Android零基础入门第3节:带你一起来聊一聊Android开发环境
- C++动态内存分配
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd 在xml报错解决方案
- 运算符
- JS解析json数据(如何将json字符串转化为数组)
- Word2vec+seq2seq实现对话系统