zoukankan      html  css  js  c++  java
  • [LeetCode]: 83: Remove Duplicates from Sorted List

    题目:

    Given a sorted linked list, delete all duplicates such that each element appear only once.

    For example,
    Given 1->1->2, return 1->2.
    Given 1->1->2->3->3, return 1->2->3.

    思路与分析:直接遍历

    代码:

        public static ListNode deleteDuplicates(ListNode head) {
            if(head == null){
                return null;
            }
            
            int intFlag = head.val;
            //ListNode listCurrent = head.next;
            ListNode listCurrent = head;
            
            
            while(listCurrent.next != null){
                if(intFlag == listCurrent.next.val){
                    if(listCurrent.next.next != null){
                        listCurrent.next = listCurrent.next.next;
                    }
                    else{
                        listCurrent.next = null;
                    }
                }
                else{
                    intFlag = listCurrent.next.val;
                    listCurrent = listCurrent.next;
                }
                
                
            }
    
            return head;
        }
  • 相关阅读:
    团队选题与评审
    消息管家
    团队展示
    功能规格说明书
    测试与优化
    git分支管理
    MVC小结
    .Net基础加强
    结对编程
    个人作业1_软件工程
  • 原文地址:https://www.cnblogs.com/savageclc26/p/4856705.html
Copyright © 2011-2022 走看看