1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * 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/