/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
//考虑没有头结点情况
if(head==null)
return head;
//tmp指向头结点
ListNode tmp=head;
while(tmp!=null){
if(tmp.next==null)//没有后续结点,跳出循环
break;
if(tmp.val==tmp.next.val){ //若有重复数值的结点则跳过
tmp.next=tmp.next.next;
}
else //不重复则tmp向后移动
tmp=tmp.next;
}
return head;
}
}
LeetCode 83:Remove Duplicates from Sorted List
最新推荐文章于 2024-05-07 23:53:17 发布