复杂链表的复制
来源:互联网 发布:禁忌搜索算法ppt 编辑:程序博客网 时间:2024/06/04 19:56
题目描述
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点)。
思路:
1、复制原链表
2、从原链表表头开始,查询每个节点的random指针指向,同时移动复制链表的指针,当找到时,移动的指针次数是一样的,
则复制链表的random指针为移动的指针
/*public class RandomListNode { int label; RandomListNode next = null; RandomListNode random = null; RandomListNode(int label) { this.label = label; }}*/public class Solution { public RandomListNode Clone(RandomListNode pHead) { if(null == pHead){ return null; }else{ RandomListNode temp1 = pHead; RandomListNode head = null; RandomListNode tail = null; RandomListNode temp = null; while(temp1!=null){ if(head == null){ head = new RandomListNode(temp1.label); tail = head; }else{ temp = new RandomListNode(temp1.label); tail.next = temp; tail = temp; } temp1 = temp1.next; } temp1 = pHead; RandomListNode temp2 = pHead; temp = head; RandomListNode temp0 = head; while(temp2!=null){ while(temp2.random != temp1){ temp1 = temp1.next; temp0 = temp0.next; } temp.random = temp0; temp2 = temp2.next; temp = temp.next; temp1 = pHead; temp0 = head; } return head; } }}
0 0
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复制复杂的链表
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 奔溃日志记录工具Crashlytics
- Linux下网络工具
- virtualbox下CentOS上网设置(NAT+Host-only)
- 如何在eclipse jee中创建Maven project并且转换为Dynamic web project
- Android API Guides---Location Strategies
- 复杂链表的复制
- 重写ListView、GridView让其在ScrollView中完整显示
- JAVA数组详解
- Android学习笔记——NDK中C++标准库、STL的配置;Include其他头文件
- MyEclipse取消自动跳到Console窗口
- 简析Chrome和Webkit的渊源
- assign、weak、strong、retain、__block、__weak
- [bzoj3956]Count 解题报告
- Win7_64Bit安装Office2007注册表1402错误的解决办法