83. Remove Duplicates from Sorted List leetcode solution in java

83.-remove-duplicates-from-sorted-list-leetcode-solution-in-java
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode temp = head;
        if(temp==null){
            return temp;
        }else if(temp.next==null){
            return temp;
        }
         ListNode prev = temp;
        temp = temp.next;
        while(temp!=null){
            if(temp.val == prev.val){
                prev.next = temp.next;
                temp = temp.next;
            }else{
                prev = temp;
                temp = temp.next;
            }
        }
        return head;
    }
}
Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
6-ways-to-earn-trust-from-consumers-who-share-data-with-your-brand-[data]

6 Ways to Earn Trust From Consumers Who Share Data With Your Brand [Data]

Next Post
how-to-use-the-hero’s-journey-for-content-marketing:-a-good-story-goes-a-long-way

How To Use the Hero’s Journey for Content Marketing: A Good Story Goes A Long Way

Related Posts