포스트

LinkedNodeList 알고리즘 (1)

정렬이 되어 있지 않은 링크드 리스트에 중복값을 제거 (버퍼를 사용하지 않고)

정렬되어 있지 않은 링크드리스트의 중복값을 제거하는 알고리즘을 구현해보았습니다.

가장 간단한 방법은 HashSet을 사용하여 구현할수 있지만 추가로 버퍼를 사용하지 않는 조건이 있어

다음과 같은 방법으로 구현하였습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class LinkedNodeList {  
  
    Node head;  
  
    LinkedNodeList() {  
        head = new Node();  
    }  
  
    static class Node {  
  
        int data;  
  
        Node next = null;  
    }  
  
    void append(int n) {  
        Node newNode = new Node();  
        newNode.data = n;  
  
        Node nowNode = head;  
  
        while (nowNode.next != null) {  
            nowNode = nowNode.next;  
        }  
        nowNode.next = newNode;  
    }  
  
    void delete(int n) {  
        Node nowNode = head;  
  
        while (nowNode.next != null) {  
            if (nowNode.next.data == n) {  
                nowNode.next = nowNode.next.next;  
            } else {  
                nowNode = nowNode.next;  
            }  
        }  
    }  
  
    void retrieve() {  
        Node nowNode = head.next;  
  
        while (nowNode.next != null) {  
            System.out.print(nowNode.data + " -> ");  
            nowNode = nowNode.next;  
        }  
  
        System.out.println(nowNode.data);  
    }  
  
    void removeDup2() {  
        Node s = head.next;  
		while (s != null && s.next != null) {
            Node r = s;  
            while (r.next != null) {  
                if (s.data == r.next.data) {  
                    r.next = r.next.next;  
                } else {  
                    r = r.next;  
                }  
            }  
            s = s.next;  
        }  
    }  
  
    void removeDup() {  
        Node s = head.next;  
        Node e = s;  
  
        if (s.next != null) {  
            e = s.next;  
        }  
  
        while (s.next != null) {  
            while (e.next != null) {  
                if (s.data == e.data) {  
                    s.next = e.next;  
                }  
                e = e.next;  
            }  
            s = s.next;  
        }  
    }  
}  
  
public class test {  
  
    // 정렬이 되어있지 않은 링크드 리스트의 중복값을 제거 > 버퍼를 사용하지 않고  
    public static void main(String[] args) {  
        LinkedNodeList ll = new LinkedNodeList();  
  
        ll.append(2);  
        ll.append(2);  
        ll.append(2);  
        ll.append(4);  
        ll.append(3);  
        ll.append(5);  
  
        ll.retrieve();  
  
        ll.removeDup2();  
  
        ll.retrieve();;  
  
    }  
}

시간 복잡도 : O(n^2) 공간 복잡도 : O(1)

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.