日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【IT笔试面试题整理】删除无序链表中重复的节点

發布時間:2023/12/15 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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

1 public static void deleteDups(LinkList head) 2 { 3 if (head == null) 4 return; 5 Link previous = head.first; 6 Link current = previous.next; 7 while (current != null) 8 { 9 Link runner = head.first; 10 while (runner != current) 11 { 12 if (runner.id == current.id) 13 { 14 Link tmp = current.next; 15 previous.next = tmp; 16 current = tmp; 17 break; 18 } 19 runner = runner.next; 20 } 21 22 if (runner == current) 23 { 24 previous = current; 25 current = current.next; 26 } 27 } 28 29 System.out.println("-----------"); 30 head.displayList(); 31 }


方法二:

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笔试面试题整理】删除无序链表中重复的节点的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。