Advertisement
CreateWithChirag

Remove Duplicate From Sorted Linked List

Feb 4th, 2023
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | Source Code | 0 0
  1. /**
  2.  * Definition for singly-linked list.
  3.  * public class ListNode {
  4.  *     int val;
  5.  *     ListNode next;
  6.  *     ListNode() {}
  7.  *     ListNode(int val) { this.val = val; }
  8.  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9.  * }
  10.  */
  11. class Solution {
  12.     public ListNode deleteDuplicates(ListNode head) {
  13.        if(head == null){
  14.            return head;
  15.        }
  16.  
  17.        ListNode curr = head;
  18.  
  19.        while(curr.next != null){
  20.            if(curr.val == curr.next.val){
  21.                curr.next = curr.next.next;
  22.            }
  23.            else{
  24.                curr = curr.next;
  25.            }
  26.        }
  27.         return head;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement