Algorithms and Data Structures/Coding Practices

AlgoExpert Remove Duplicates from LinkedList

brightlightkim 2022. 7. 14. 10:57

import java.util.*;

class Program {
  // This is an input class. Do not edit.
  public static class LinkedList {
    public int value;
    public LinkedList next;

    public LinkedList(int value) {
      this.value = value;
      this.next = null;
    }
  }

  public LinkedList removeDuplicatesFromLinkedList(LinkedList linkedList) {
    LinkedList root = linkedList;
    while(linkedList.next != null){
      if (linkedList.value == linkedList.next.value){
        if (linkedList.next.next == null){
          linkedList.next = null;
        } else {
          LinkedList tmp = linkedList.next;
          linkedList.next = linkedList.next.next;
          tmp.next = null;
        }
      } else {
        linkedList = linkedList.next;
      }
    }
    return root;
  }
}

'Algorithms and Data Structures > Coding Practices' 카테고리의 다른 글

Kattis 3D printer  (0) 2022.07.14
LeetCode 15. 3Sums  (0) 2022.07.14
AlgoExpert Tandem Bike  (0) 2022.07.14
LeetCode 74. Search a 2D Matrix  (0) 2022.07.13
AlgoExpert Class Photos  (0) 2022.07.13