Leet Code OJ 83. Remove Duplicates from Sorted List [Difficulty: Easy]

题目:

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.

翻译:

给定一个排序号的链表,删除所有的重复元素,保证每个元素只出现一次。

分析:

在当前节点删除下一节点,会比较容易操作,只需要修改next指针。

代码:

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

        }
        return head;
    }
}
时间: 2024-10-14 06:52:41

Leet Code OJ 83. Remove Duplicates from Sorted List [Difficulty: Easy]的相关文章

Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]

题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array

LeetCode OJ 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. 1 public class Solution { 2 public ListNode deleteDuplicates(

Leet Code OJ 102. Binary Tree Level Order Traversal [Difficulty: Easy]

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, return its level order traversal as: [ [3], [9,20], [15,7] ] 翻译: 给定一个二叉树,返回它的节

<LeetCode OJ> 83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: Easy 题目意思:如今有一个已经排好顺序的链表,删除全部反复的节点.使每一个节点都仅仅出现一次! Given a sorted linked list, delete all duplicates such that each element appear only once. For exampl

leetCode 83. Remove Duplicates from Sorted List 链表

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. 题目大意: 去除有序链表内部相同元素,即相同

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta

<LeetCode OJ> 26. Remove Duplicates from Sorted Array

26. Remove Duplicates from Sorted Array My Submissions Question Total Accepted: 104150 Total Submissions: 322188 Difficulty: Easy Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

leetcode:83 Remove Duplicates from Sorted List-每日编程第十六题

Remove Duplicates from Sorted List Total Accepted: 89961 Total Submissions: 253975 Difficulty: Easy 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-&g

【一天一道LeetCode】#83. Remove Duplicates from Sorted List

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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-&