关闭

【Leetcode】Copy List with Random Pointer

242人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/copy-list-with-random-pointer/

题目:

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

思路:

先拷贝原链表值,然后找到原链表每个结点的随机指针在链表中的位置,找到拷贝链表的随机指针的指向。

算法:

	public RandomListNode copyRandomList(RandomListNode head) {
		// head2表示拷贝链表表头,t指向拷贝链表最后一个结点
		RandomListNode p = head, head2 = null, q = null, t = head2;
		// ====建立链表值的拷贝链表
		while (p != null) {
			q = new RandomListNode(p.label);
			if (head2 == null) {
				head2 = q;
				t = head2;
			} else {
				t.next = q;
				t = t.next;
			}
			p = p.next;
		}
		// ====建立拷贝链表的随机指针的指向
		p = head;
		RandomListNode pp = head2, target, tmp;
		while (p != null) {
			q = head2; // q指向拷贝链表
			tmp = head;// tmp指向原链表
			target = p.random; // target是原链表的p结点的随机指针
			if (target == null) {
				pp.random = null;
			} else {
				// 在原链表上,从头开始查找和随机指针相等的结点,找到后因为拷贝指针位移和原指针相等,所以此时q指向的就是拷贝链表结点的随机指针的结点
				while (tmp != null) {
					if (tmp == target) {
						pp.random = q;// 找到随机指针指向的结点时,q指向的就是拷贝链表的random point
						break;
					} else {
						tmp = tmp.next;// 原链表和拷贝链表指针同时移动一步
						q = q.next;
					}
				}
			}
			p = p.next; // 原和拷贝链表保持相同位移
			pp = pp.next;
		}

		return head2;
	}


0
0
查看评论

LeetCode138 Copy List with Random Pointer(深度复制带有随机指针的链表) Java题解

题目: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return...
  • u012249528
  • u012249528
  • 2015-07-28 21:32
  • 955

【LeetCode-面试算法经典-Java实现】【138-Copy List with Random Pointer(拷贝有随机指针的单链表)】

【138-Copy List with Random Pointer(拷贝有随机指针的单链表)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  A linked list is given such that each node contains an additiona...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-18 06:26
  • 2180

Copy List with Random Pointer -- LeetCode

原题链接: http://oj.leetcode.com/problems/copy-list-with-random-pointer/  这是一道链表操作的题目,要求复制一个链表,不过链表的每个结点带有一个随机指针,指向某一个结点。 我们先介绍一种比较直接的算法,思路是先按...
  • linhuanmars
  • linhuanmars
  • 2014-03-29 05:10
  • 12430

[Leetcode 138] Copy List with Random Pointer

题目 复制带有随机指针的链表:一个单链表除了next指针外还有一个random指针随机指向任何一个元素(可能为空) 《剑指offer》上的面试题26 分析 代码
  • yiting52
  • yiting52
  • 2015-07-27 10:42
  • 980

[Leetcode][python]Copy List with Random Pointer

题目大意一个链表中的每一个节点都有一个额外的随机指针,指向链表中的任意节点或空节点。对这个链表进行深拷贝。(要拷贝随即指针)解题思路有两种思路,参考: http://bookshadow.com/weblog/2015/07/31/leetcode-copy-list-random-pointer...
  • qqxx6661
  • qqxx6661
  • 2017-11-21 02:47
  • 49

**Leetcode_copy-list-with-random-pointer (c++ and python version)

地址:http://oj.leetcode.com/problems/copy-list-with-random-pointer/ A linked list is given such that each node contains an additional random pointer w...
  • flyupliu
  • flyupliu
  • 2014-03-22 22:18
  • 534

【LeetCode with Python】 Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep cop...
  • nerv3x3
  • nerv3x3
  • 2014-09-21 17:35
  • 2306

leetcode_c++:哈希: Copy List with Random Pointer(138)

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy ...
  • mijian1207mijian
  • mijian1207mijian
  • 2016-07-06 14:49
  • 112

Leetcode Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep c...
  • u011472124
  • u011472124
  • 2016-07-15 03:23
  • 83

[Leetcode]Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep cop...
  • NoooName
  • NoooName
  • 2014-12-31 13:14
  • 166
    个人资料
    • 访问:568950次
    • 积分:8678
    • 等级:
    • 排名:第2678名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论