[Leetcode] Copy List with Random Pointer (Java)
2014-02-15 15:51
351 查看
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.
带随机指针的单链表复制
先整理成 head->copyhead->seconde->copysecond->……->last->copylast的形式
然后遍历链表处理random指针
最后进行拆分
Return a deep copy of the list.
带随机指针的单链表复制
先整理成 head->copyhead->seconde->copysecond->……->last->copylast的形式
然后遍历链表处理random指针
最后进行拆分
public class Solution { public RandomListNode copyRandomList(RandomListNode head) { if(head==null) return null; RandomListNode cur = head; while(cur!=null){ RandomListNode tmp = new RandomListNode(cur.label); tmp.next=cur.next; cur.next=tmp; cur=tmp.next; } cur = head; while(cur!=null){ RandomListNode copy = cur.next; if(cur.random!=null) copy.random = cur.random.next; cur=copy.next; } RandomListNode ret = new RandomListNode(-1); RandomListNode pre = ret; cur = head; while(cur!=null){ pre.next = cur.next; cur.next = cur.next.next; pre=pre.next; cur=cur.next; } return ret.next; } }
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【143-Copy List with Random Pointer(有随机指针的链表复制)】
- Copy List with Random Pointer leetcode java
- Copy List with random pointer--Leetcode(Java)
- LeetCode138 Copy List with Random Pointer(深度复制带有随机指针的链表) Java题解
- Copy List with Random Pointer leetcode java
- [leetcode-138]Copy List with Random Pointer(java)
- [leetcode] Copy List with Random Pointer, Java
- 【LeetCode-面试算法经典-Java实现】【138-Copy List with Random Pointer(拷贝有随机指针的单链表)】
- leetcode:Copy List with Random Pointer 细致分析,以及代码实现(JAVA版本)
- [Leetcode][JAVA] Clone Graph, Copy List with Random Pointer
- JAVA学习44_leetCode解题报告之Copy List with Random Pointer
- Java for LeetCode 138 Copy List with Random Pointer
- leetcode - Copy List with Random Pointer
- LeetCode: Copy List with Random Pointer
- Leetcode Copy List with Random Pointer
- **Leetcode_copy-list-with-random-pointer (c++ and python version)
- leetCode解题报告之Copy List with Random Pointer
- Leetcode Copy List with Random Pointer(面试题推荐)
- LeetCode 解题报告 Copy List With Random Pointer
- Leetcode: Copy List with Random Pointer