Very simple java solution


  • 0
    D
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if(head!=null){
                ListNode curr=head,next;
                while(curr.next!=null){
                    next=curr.next;
                    if(curr.val==next.val)curr.next=next.next;
                    else curr=next;
                }
            }
            return head;
        }
    }

  • 0
    7

    your code only can delete Adjacent duplicate nodes,but far from each other.


  • 0
    D

    If the linked list is sorted, why does the algorithm have to check for anything but adjacent duplicates?


  • 0
    7

    I see.i overlook this conditions that the linked list is sorded.Thank you for your reply.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.