• leetcode--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.

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if(head != null){
    			ListNode currentFirst = head;
    			ListNode currentNode = head.next;
    			while(currentNode != null){
    				if(currentFirst.val != currentNode.val){
    					currentFirst.next = currentNode;
    					currentFirst = currentFirst.next;
    				}
    				currentNode = currentNode.next;
    			}
    			if(currentFirst.next != null)
    			    currentFirst.next = null;
    		}
    		return head;    
        }
    }
    

      

  • 相关阅读:
    使用Delphi调用条形码控件BarTender打印标签
    我看过的书
    语法规则
    智能家居
    HAL库ADC的DMA采集
    HAL库串口中断接收
    触动心灵的一句话
    摄影技巧
    中国茶道
    单片机延时函数
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3767603.html
Copyright © 2020-2023  润新知