复习-JZ56 删除链表中重复的结点

删除链表中重复的结点

https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef?tpId=13&&tqId=11209&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

2021年9月12日08:26:02
2021年9月12日08:36:58

while(cur!= null && cur.next != null){ 这块最开始写的是 || 后来改成了 &&

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

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode virtualhead = new ListNode(-1);
        virtualhead.next = pHead;

        ListNode pre = virtualhead;
        ListNode cur = pHead;

        while(cur!= null && cur.next != null){
            if(cur.val == cur.next.val){
                while(cur.next != null && cur.val == cur.next.val){
                    cur = cur.next;
                }
                pre.next = cur.next;
                cur = cur.next;
            }
            else{
                pre = cur;
                cur = cur.next;
            }
        }
        return virtualhead.next;
    } 
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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