题解 | #删除有序链表中重复的元素-I#TOP15

思路:
1.对于当前节点,利用while循环删掉后面值和他相等的节点即可


/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        if(head == null || head.next == null){
            return head;
        }
        ListNode  currentNode = head;
        while(currentNode != null){
            ListNode next = currentNode.next;
            while(next != null && currentNode.val == next.val){
                currentNode.next = next.next;
                next = next.next;
            }
            currentNode = currentNode.next;
        }
        return head;
    }
}
面试必刷TOP101 文章被收录于专栏

面试必刷TOP101

全部评论

相关推荐

06-18 13:28
已编辑
门头沟学院 Web前端
爱睡觉的冰箱哥:《给予你300的工资》,阴的没边了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 15:07
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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