题目链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/ 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 class
题目链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 class Solution { 10 public ListNode deleteDuplicates(ListNode head) { 11 if(head==null) 12 return null; 13 ListNode p=head; 14 while(p.next !=null) 15 { 16 if(p.val==p.next.val) 17 { 18 if(p.next.next!=null)//防止是最后一个出现空的情况 19 p.next = p.next.next; 20 else 21 p.next=null; 22 } 23 else 24 { 25 p=p.next; 26 } 27 } 28 return head; 29 } 30 }