美文网首页
83. Remove Duplicates from Sorte

83. Remove Duplicates from Sorte

作者: 苏州城外无故人 | 来源:发表于2019-02-22 13:07 被阅读0次
链表去重

思路:复制链表的head指针,依次比较当前指针的值和下一个是否相等


public ListNode deleteDuplicates(ListNode head) {
        ListNode current = head;
        while (current != null && current.next != null) {
            if (current.val == current.next.val) {
                current.next = current.next.next;
            } else {
                current = current.next;
            }
        }
        return head;
    }

相关文章

网友评论

      本文标题:83. Remove Duplicates from Sorte

      本文链接:https://www.haomeiwen.com/subject/yoteyqtx.html