Skip to content

Instantly share code, notes, and snippets.

@mikeyang01
Created February 21, 2019 14:51
Show Gist options
  • Select an option

  • Save mikeyang01/53c81c80d69d6359b24546cff49f1836 to your computer and use it in GitHub Desktop.

Select an option

Save mikeyang01/53c81c80d69d6359b24546cff49f1836 to your computer and use it in GitHub Desktop.
import java.util.*;
public class LinkedList_del_Duplicatation {
public class ListNode {
public int val;
public ListNode next;
// constructor
public ListNode(int val) {
this.val = val;
}
// for print
@Override
public String toString() {
return Integer.toString(val);
}
}
//删除单链表中的重复节点
public static void deleteDuplication(ListNode node) {
Hashtable table = new Hashtable();
ListNode previous = null;
while (node != null) {
if (table.containsKey(node.val)) {
previous.next = node.next;
} else {
table.put(node.val, true);
previous = node;
}
node = node.next;
}
}
public static void main(String[] args) {
LinkedList_del_Duplicatation execute = new LinkedList_del_Duplicatation();
//-构造单链表start----
int[] num = {1, 2, 2, 3, 4, 5};
ListNode head = execute.new ListNode(num[0]);
ListNode pre = head;
for (int i = 1; i < num.length; i++) {
ListNode node = execute.new ListNode(num[i]);
pre.next = node;
pre = node;
}
execute.deleteDuplication(head);
while (head.next != null) {
System.out.print(head.val + " ");
head = head.next;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment