天天看点

OJ题-删除链表中重复的结点(牛客网)

OJ题-删除链表中重复的结点(牛客网)
思路:
OJ题-删除链表中重复的结点(牛客网)
/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ListNode deleteDuplication(ListNode pHead)
    {
          if (pHead == null) {
             return null;
         }		
        ListNode prev = null;
        ListNode p1 = pHead;
        ListNode p2 = pHead.next;
        while (p2 != null) {
            if (p1.val != p2.val) {
                prev = p1;
                p1 = p2;
                p2 = p2.next;
            } else {
                while (p2 != null && p2.val == p1.val) {
                    p2 = p2.next;
                }
                
                if (prev == null) {
                    pHead = p2;
                } else {
                    prev.next = p2;
                }
                p1 = p2;
                if (p2 != null) {
                    p2 = p2.next;
                }
            }
        }     
        return pHead;
    }
}