题解 | #删除链表中重复的结点#
删除链表中重复的结点
https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } } */ public class Solution { public ListNode deleteDuplication(ListNode pHead) { ListNode head = new ListNode(-1); head.next = pHead; // 上⼀个元素指针为pre ListNode pre = head, cur = pHead; while (cur != null) { if (cur.next != null && cur.val == cur.next.val) { cur = cur.next; while (cur.next != null && cur.val == cur.next.val) { cur = cur.next; } cur = cur.next; pre.next = cur; } else { pre = cur; cur = cur.next; } } return head.next; } }
解题思想:
* 方式一:借助map统计次数,然后遍历形成新链表(排除重复链表节点)
* 方式二:自身遍历去除重复节点,改变指针指向
#算法##算法笔记#