【IT笔试面试题整理】删除无序链表中重复的节点
生活随笔
收集整理的這篇文章主要介紹了
【IT笔试面试题整理】删除无序链表中重复的节点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【試題描述】定義一個函數,輸入一個鏈表,刪除無序鏈表中重復的節點
【參考代碼】
方法一:
Without a buffer, we can iterate with two pointers: “current” does a normal iteration, while?
“runner” iterates through all prior nodes to check for dups Runner will only see one dup?
per node, because if there were multiple duplicates they would have been removed already
方法二:
If we can use a buffer, we can keep track of elements in a hashtable and remove any dups:
public static void deleteDups2(LinkList head){if (head == null)return;Hashtable table = new Hashtable();Link previous = null;Link current = head.first;while (current != null){if (table.containsKey(current.id))previous.next = current.next;else{table.put(current.id, true);previous = current;}current = current.next;}System.out.println("-----------");head.displayList();}?
總結
以上是生活随笔為你收集整理的【IT笔试面试题整理】删除无序链表中重复的节点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 比亚迪元PLUS在海外攻城略地 但是全球
- 下一篇: 【IT笔试面试题整理】判断一个树是否是另