题解 | #牛群排列去重#

判断是否为null,直接返回

不能null,那么就遍历,获取下一节点,与当前节点比较,如果值相同,那么以下一节点为起始点,往下找不等于当前节点的值的指针

如果找到,当前节点下一节点指向找到的节点,当前节点指向找到的节点继续,直到为null

public ListNode deleteDuplicates (ListNode head) {
    // write code here
    if (head == null) {
        return null;
    }
    ListNode tem = head;
    while (head != null && head.next != null) {
        if (head.val == head.next.val) {
            head.next = head.next.next;

        } else {
            head = head.next;
        }
    }
    return tem;
}
#链表##牛群排列去重#
全部评论

相关推荐

01-26 19:51
门头沟学院 Java
isabener:怎么感觉像群发的呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务