|
| 1 | +package algorithm.linkedlist; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.Stack; |
| 8 | + |
| 9 | +import static org.hamcrest.CoreMatchers.is; |
| 10 | +import static org.junit.Assert.assertThat; |
| 11 | + |
| 12 | +public class SingleLinkedListTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + public void test() { |
| 16 | + assertThat("", is(testFunction())); |
| 17 | + } |
| 18 | + |
| 19 | + public String testFunction() { |
| 20 | + return ""; |
| 21 | + } |
| 22 | + |
| 23 | + public class Node { |
| 24 | + int data; |
| 25 | + Node next; |
| 26 | + } |
| 27 | + |
| 28 | + private class SingleLinkedList { |
| 29 | + private Node head; |
| 30 | + |
| 31 | + public void addToHead(int n) { |
| 32 | + Node newNode = new Node(); |
| 33 | + newNode.data = n; |
| 34 | + head.next = newNode; |
| 35 | + head = newNode; |
| 36 | + } |
| 37 | + |
| 38 | + /* |
| 39 | + TASK |
| 40 | + 첫번째 원소를 제거하라. |
| 41 | + */ |
| 42 | + |
| 43 | + public void removeFirst() { |
| 44 | + if (head == null) { |
| 45 | + throw new RuntimeException("Not found"); |
| 46 | + } |
| 47 | +// head = head.next; |
| 48 | + Node temp = head; |
| 49 | + head = null; |
| 50 | + head = temp.next; |
| 51 | + } |
| 52 | + |
| 53 | + /* |
| 54 | + TASK |
| 55 | + 중복된 원소를 제거하라. |
| 56 | + */ |
| 57 | + |
| 58 | + public void removeDuplicateElm() { |
| 59 | + Set<Integer> set = new HashSet<>(); |
| 60 | + Node prev = null; |
| 61 | + Node start = head; |
| 62 | + while (start != null) { |
| 63 | + if (set.contains(start.data)) { |
| 64 | + if (prev == null) { |
| 65 | + break ; |
| 66 | + } |
| 67 | + prev.next = start.next; |
| 68 | + } else { |
| 69 | + set.add(start.data); |
| 70 | + prev = start; |
| 71 | + } |
| 72 | + start = start.next; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /* |
| 77 | + TASK |
| 78 | + 역순으로 출력하라. |
| 79 | + */ |
| 80 | + |
| 81 | + public void reverse() { |
| 82 | + Node prev = null; |
| 83 | + Node start = head; |
| 84 | + Node next = null; |
| 85 | + while (start != null) { |
| 86 | + next = start.next; |
| 87 | + start.next = prev; |
| 88 | + prev = next; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /* |
| 93 | + TASK |
| 94 | + k번째 원소를 찾아라. |
| 95 | + */ |
| 96 | + |
| 97 | + public Node kthToLast(int k) { |
| 98 | + Node result = head; |
| 99 | + if (k < 0) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + int count = 0; |
| 103 | + while(count < k) { |
| 104 | + result = head.next; |
| 105 | + head = head.next; |
| 106 | + count++; |
| 107 | + } |
| 108 | + return result; |
| 109 | + } |
| 110 | + |
| 111 | + /* |
| 112 | + TASK |
| 113 | + 회문인지 판단하라. |
| 114 | + */ |
| 115 | + |
| 116 | + public boolean isPlaindrome() { |
| 117 | + Stack<Node> stack = new Stack<>(); |
| 118 | + Node node1 = head; |
| 119 | + Node node2 = head; |
| 120 | + while (node1 != null && node2 != null) { |
| 121 | + stack.push(node1); |
| 122 | + node1 = head.next; |
| 123 | + node2 = head.next.next; |
| 124 | + } |
| 125 | + if (node2 != null) { //홀수인 경우 |
| 126 | + node1 = node1.next; |
| 127 | + } |
| 128 | + |
| 129 | + while (node1 != null) { |
| 130 | + if (stack.pop().data != node1.data) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + node1 = node1.next; |
| 134 | + } |
| 135 | + return true; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments