leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

remove-duplicates.dart (1130B)


      1 //This is a simple algorithm that removes duplicate values in a singly linked list that
      2 //is already sorted. My algorithm has a worst case time complexity of O(n) where n is the length
      3 //of the singly linked list.
      4 //Time: 272ms Beats: 90.38%
      5 //Memory: 143.7MB Beats: 40.38%
      6 
      7 
      8 
      9 void main(){
     10     ListNode ln1 = ListNode();
     11     ListNode ln2 = ListNode();
     12     ListNode ln3 = ListNode();
     13     ln1.next = ln2;
     14     ln2.next = ln3;
     15     ln1.val = 0;
     16     ln2.val = 100;
     17     ln3.val = 100;
     18     Solution sol = new Solution();
     19     ListNode? head = sol.deleteDuplicates(ln1);
     20     while(head != null){
     21         print(head.val);
     22         head = head.next;
     23     }
     24 }
     25 
     26 class ListNode {
     27     int val = 0;
     28     ListNode? next;
     29 }
     30 
     31 class Solution {
     32   ListNode? deleteDuplicates(ListNode? head) {
     33       ListNode? first = head;
     34       ListNode? second = head?.next;
     35       while(true){
     36           second = head?.next;
     37           if(head == null){
     38               break;
     39           }
     40           if(head?.val == second?.val){
     41               head?.next = second?.next;
     42           }
     43           else{
     44               head = second;
     45           }
     46 
     47       }
     48       return first;
     49   }
     50 }