1、题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
2、代码实现
import java.util.HashMap;public class Solution { public ListNode deleteDuplication(ListNode pHead) { //1、临界值的判断: if (pHead == null) { return null; } HashMaphashMap = new HashMap<>(); ListNode temp = pHead; while (temp != null) { if (hashMap.containsKey(temp.val) == true) { hashMap.put(temp.val, hashMap.get(temp.val) + 1); } else { hashMap.put(temp.val, 1); } temp = temp.next; } temp = pHead; ListNode newHead = new ListNode(-1); ListNode NTemp = newHead; while (temp != null) { if (hashMap.get(temp.val) > 1) { temp = temp.next; } else { NTemp.next = new ListNode(temp.val); NTemp = NTemp.next; temp = temp.next; } } return newHead.next; }}