题解 | #删除有序链表中重复的元素-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

全部评论

相关推荐

明天不下雨了:这个项目 这个简历 这个模板 莫不是一个开源的
点赞 评论 收藏
分享
11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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