生活随笔
收集整理的這篇文章主要介紹了
数据结构与算法--复杂链表的复制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
復雜鏈表的復制
- 題目:實現一個函數complexListNode 復制一個復雜鏈表。在鏈表中,每個節點除了有一個next指針指向下一個節點,還有另外一個before節點,before節點指向鏈表中任意一個節點,或者null節點。
- 鏈表節點定義使用之前文章 數據結構與算法–鏈表實現以及應用 中鏈表節點的定義,以及鏈表中其他方法實現都沿用以上文章中有詳細的說明。
- 如下節點定義:
public class ListNode implements Comparable<ListNode> {private Integer value
;private ListNode next
;private ListNode before
;
......
}
public static ListNode
buildComplexListNode(){Random random
= new Random();ListNode pHead
= new ListNode(random
.nextInt(100));ListNode
[] nodeArray
= new ListNode[20];for (int i
= 0; i
< 20; i
++) {ListNode newNode
= new ListNode(random
.nextInt(100));nodeArray
[i
] = newNode
;ListNode headNode
= pHead
;while (headNode
.getNext() != null
){headNode
= headNode
.getNext();}headNode
.setNext(newNode
);}print(pHead
);ListNode headNode
= pHead
;while (headNode
.getNext() != null
){headNode
.setBefore(nodeArray
[random
.nextInt(nodeArray
.length
-1)]);headNode
= headNode
.getNext();}return pHead
;}public static ListNode
complexListNode(ListNode pHead
){if(pHead
== null
|| pHead
.getNext() == null
){return pHead
;}Map
<ListNode, ListNode> oldToNewNode
= new HashMap<>();ListNode complexNode
= pHead
;ListNode newHead
= null
;ListNode myHeadNode
= null
;while (complexNode
!= null
){ListNode createNode
= new ListNode(complexNode
.getValue());oldToNewNode
.put(complexNode
, createNode
);if(newHead
== null
){newHead
= createNode
;myHeadNode
= newHead
;}else {myHeadNode
.setNext(createNode
);myHeadNode
= myHeadNode
.getNext();}complexNode
= complexNode
.getNext();}ListNode beforeComplexNode
= pHead
;ListNode beforeSetNode
= newHead
;while (beforeComplexNode
.getNext() != null
&& beforeSetNode
.getNext() != null
){if(oldToNewNode
.containsKey(beforeComplexNode
.getBefore())){beforeSetNode
.setBefore(oldToNewNode
.get(beforeComplexNode
.getBefore()));}beforeComplexNode
= beforeComplexNode
.getNext();beforeSetNode
= beforeSetNode
.getNext();}return newHead
;}
public static ListNode
complexListNode_2(ListNode pHead
){if(pHead
== null
|| pHead
.getNext() == null
){return pHead
;}ListNode compleNode
= pHead
;while (compleNode
.getNext() != null
){ListNode createNode
= new ListNode(compleNode
.getValue());createNode
.setNext(compleNode
.getNext());compleNode
.setNext(createNode
);compleNode
= compleNode
.getNext().getNext();}ListNode beforeCompleNode
= pHead
;while (beforeCompleNode
.getNext() != null
){if(beforeCompleNode
.getBefore() != null
){beforeCompleNode
.getNext().setBefore(beforeCompleNode
.getBefore().getNext());}beforeCompleNode
= beforeCompleNode
.getNext().getNext();}ListNode fixCompleNode
= pHead
;ListNode resultHead
= null
;ListNode resultNode
= null
;while (fixCompleNode
.getNext() != null
){if(resultHead
== null
){resultHead
= fixCompleNode
.getNext();resultNode
= resultHead
;}else {resultNode
.setNext(fixCompleNode
.getNext());resultNode
= resultNode
.getNext();}fixCompleNode
= fixCompleNode
.getNext().getNext();}return resultHead
;}public static void printListNode(ListNode pHead
){ListNode printNode
= pHead
;while (printNode
.getNext() != null
){System
.out
.print("value: " + printNode
.getValue());System
.out
.print(", ");System
.out
.print("before: " + (printNode
.getBefore() != null
? printNode
.getBefore().getValue() : ""));System
.out
.print(", ");System
.out
.print("next: " + (printNode
.getNext() != null
? printNode
.getNext().getValue() : ""));printNode
= printNode
.getNext();System
.out
.println();}}public static void main(String
[] args
) {ListNode oldListNode
= buildComplexListNode();System
.out
.println("oldListNode:");printListNode(oldListNode
);System
.out
.println("newListNode:");ListNode mewListNode
= complexListNode(oldListNode
);printListNode(mewListNode
);ListNode newListNode_2
= complexListNode_2(oldListNode
);System
.out
.println("newListNode_2:");printListNode(newListNode_2
);}
- 以上代碼中其他未定義方法都沿用 數據結構與算法–鏈表實現以及應用 中對應的方法。
上一篇:數據結構與算法-- 二叉樹中和為某一值的路徑
下一篇:數據結構與算法–二叉查找樹轉順序排列雙向鏈表
總結
以上是生活随笔為你收集整理的数据结构与算法--复杂链表的复制的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。