오늘도 상쾌한 마음으로 백준 문제를 풀었습니다. 링크드리스트(Linked List) 문제를 배열없이 Linked List를 이용해 풀었습니다.
코드 설명 코드만 봐도 쉽게 이해 할수 있게 객체 지향적으로 작성되었으며 코드 사용시 출처 링크를 남겨주시면 감사하겠습니다. 백준 1021번 문제 링크 More info: 회전하는 큐 자바는 여기서 배웠습니다 More info: 유데미 강좌(영어)/자바강의 링크드리스트 및 자료구조의 경우 아래에서 배웠습니다 More info: 유데미 강좌(영어)/자바자료구조 알고리즘
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 import java.util.Scanner;public class Main { public static class Node { int data; Node next; Node previous; public void displayNode () { System.out.println("{ " + data + " }" ); } } public static class CustomLinkedList { Node first; Node last; public CustomLinkedList () { this .first = null ; this .last = null ; } public void InsertFirst (int data) { Node newNode = new Node(); newNode.data = data; if (IsEmpty()) { last = newNode; }else { first.previous = newNode; } newNode.next = first; this .first = newNode; } public void InsertLast (int data) { Node newNode = new Node(); newNode.data = data; if (IsEmpty()) { first = newNode; }else { last.next = newNode; } newNode.previous = last; this .last = newNode; } public Node DeleteFirstNode () { Node temp = first; if (first.next == null ){ last = null ; }else { first.next.previous = null ; } first = first.next; return temp; } public boolean IsEmpty () { return (first == null ); } public int StepsFromFirst (int data) { int k = 0 ; Node Current = first; while (Current != null ) { if (Current.data == data) { return k; } Current = Current.next; k++; } return k; } public void RearrangeListToHaveDataFirst (int data) { Node Current = first; while (Current != null ){ if (Current.data == data){ break ; } if (first.next == null ){ last = null ; }else { first.next.previous = null ; } first = first.next; Node newNode = new Node(); newNode.data = Current.data; if (IsEmpty()) { first = newNode; }else { last.next = newNode; } newNode.previous = last; this .last = newNode; Current = Current.next; } } public void displayForward () { System.out.println("List (first --> last) " ); Node Current = first; while (Current != null ){ Current.displayNode(); Current = Current.next; } System.out.println(); } } public static void main (String[] args) throws Exception { CustomLinkedList cll = new CustomLinkedList(); Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int M = sc.nextInt(); for (int i =N;i>0 ;i--){ cll.InsertFirst(i); } int counter = 0 ; for (int i=0 ;i<M;i++){ int a = sc.nextInt(); int k = cll.StepsFromFirst(a); if ((N-i-k) > k){ counter += k; }else { counter += (N-i-k); } cll.RearrangeListToHaveDataFirst(a); cll.DeleteFirstNode(); } System.out.println(counter); } }
글을 마치며 인간은 뛰어넘은 역경의 숫자만큼 강해진다.
인간은 뛰어넘은 역경의 숫자만큼 강해진다.
그 숫자가 많으면 많을수록,
어떠한 상황에서도 지지 않는 강한 사람이 된다.
그러니까 인생에서 성공하는 사람이 된다는 것은
역경을 많이 극복한다는 것과 같은 뜻이기도 하다.
<기타가와 야스시, ‘편지가게’>
모두들 화이팅 하셔서 훌륭한 프로그래머가 되시길 바라겠습니다.