/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode deleteDuplicates(ListNode pHead) { if(pHead==null) { return null; } ListNode head=new ListNode(0); head.next=pHead; ListNode p=head; while(p.next!=null&&p.next.next!=null) { ListNode node1=p.next; ListNode node2=node1.next; if(node1.val==node2.val) { p.next=node2.next; }else{ p=p.next; } } return pHead; } }
题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/submissions/