删除排序链表的的重复节点

xiaoxiao2021-02-28  108

public ListNode deleteDuplication(ListNode pHead) { if (pHead == null) return null; ListNode p = pHead;//遍历链表的当前节点 ListNode n = new ListNode(0);//新加一个头节点 ListNode pre = n;//当前不重复链表的尾节点 while (p != null) { ListNode q = p.next; if (q == null) {pre.next = p;break;} if (q.val == p.val) { while (q != null && q.val == p.val) { q = q.next; } p = q; } else { pre.next = p; pre = p; pre.next = null; p = q; } } return n.next; }
转载请注明原文地址: https://www.6miu.com/read-72366.html

最新回复(0)