Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
备份头指针,分3种情况判断即可
public ListNode deleteDuplicates(ListNode head) { if (head==null || head.next==null) return head; ListNode index =head; while (index.next!=null){ if (index.next.val==index.val && index.next.next!=null){ index.next=index.next.next; }else if (index.next.val==index.val && index.next.next==null){ index.next=null; }else { index=index.next; } } return head; }
时间: 2024-10-12 23:12:21