0
点赞
收藏
分享

微信扫一扫

面试必刷TOP101:15、删除有序链表中重复的元素-I

题目

面试必刷TOP101:15、删除有序链表中重复的元素-I_java

题解

import java.util.*;
public class Solution {
    public ListNode deleteDuplicates (ListNode head) {
        //空链表
        if(head == null) 
            return null;
        //遍历指针
        ListNode cur = head; 
        //指针当前和下一位不为空
        while(cur != null && cur.next != null){ 
            //如果当前与下一位相等则忽略下一位
            if(cur.val == cur.next.val) 
                cur.next = cur.next.next;
            //否则指针正常遍历
            else 
                cur = cur.next;
        }
        return head;
    }
}

举报

相关推荐

0 条评论