|
| 1 | + |
| 2 | +import java.util.HashMap; |
| 3 | +import java.util.Map; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + public static void main(String[] args) { |
| 9 | +// Output.printf(Solution.deleteDuplicates(Input.getHeadList().getHead())); |
| 10 | +// Output.printf(Solution.mergeTwoLists(Input.getHeadList().getHead(),Input.getHeadList().getHead())); |
| 11 | +// Output.printf(Solution.swapPairs(Input.getHeadList().getHead())); |
| 12 | +// HeadList list = Input.getHeadList(); |
| 13 | +// Output.printf(HeadList.searchNode(Solution.detectCycle(HeadList.createCycle(Input.getPositionOrK(),list)),list)); |
| 14 | +// Output.printf(Solution.reverseKGroup(Input.getHeadList().getHead(),Input.getPositionOrK())); |
| 15 | + |
| 16 | + |
| 17 | + } |
| 18 | + |
| 19 | +} |
| 20 | + |
| 21 | +/* Here is Interface class |
| 22 | + Name: ListNode |
| 23 | + */ |
| 24 | +class Solution{ |
| 25 | + static HeadList.ListNode deleteDuplicates(HeadList.ListNode head) { |
| 26 | + Map<Integer,HeadList.ListNode> map = new HashMap<>(); |
| 27 | + HeadList.ListNode tmp = head; |
| 28 | + while (tmp != null){ //JC: tmp.next == null |
| 29 | + if(map.get(tmp.val) == null){ //JC: map.get() return something |
| 30 | + map.put(tmp.val,tmp); |
| 31 | + } |
| 32 | + else HeadList.delete(head,tmp); |
| 33 | + tmp = tmp.next; |
| 34 | + } |
| 35 | + return head; |
| 36 | + } |
| 37 | + static HeadList.ListNode mergeTwoLists(HeadList.ListNode l1, HeadList.ListNode l2){ |
| 38 | + HeadList list = new HeadList(); |
| 39 | + HeadList.ListNode tmp1 = l1.next; //because of head node. |
| 40 | + HeadList.ListNode tmp2 = l2.next; |
| 41 | + while (true){ |
| 42 | + if (tmp1 == null){ |
| 43 | + for( ; tmp2 != null; tmp2 = tmp2.next){ |
| 44 | + list.insert(tmp2.val); |
| 45 | + } |
| 46 | + break; |
| 47 | + } |
| 48 | + else if (tmp2 == null){ |
| 49 | + for( ; tmp1 != null; tmp1 = tmp1.next){ |
| 50 | + list.insert(tmp1.val); |
| 51 | + } |
| 52 | + break; |
| 53 | + } |
| 54 | + else if (tmp1.val > tmp2.val){ |
| 55 | + list.insert(tmp2.val); |
| 56 | + tmp2 = tmp2.next; |
| 57 | + } |
| 58 | + else { |
| 59 | + list.insert(tmp1.val); |
| 60 | + tmp1 = tmp1.next; |
| 61 | + } |
| 62 | + } |
| 63 | + return list.getHead(); |
| 64 | + } |
| 65 | + static HeadList.ListNode swapPairs(HeadList.ListNode head){ |
| 66 | + HeadList.ListNode pre, tmp, tail; |
| 67 | + pre = head; |
| 68 | + while (true){ |
| 69 | + if (pre.next == null) break; |
| 70 | + if (pre.next.next == null) break; |
| 71 | + tmp = pre.next; |
| 72 | + tail = pre.next.next.next; |
| 73 | + pre.next = pre.next.next; |
| 74 | + pre.next.next = tmp; |
| 75 | + tmp.next = tail; |
| 76 | + pre = pre.next.next; |
| 77 | + } |
| 78 | + return head; |
| 79 | + } |
| 80 | + static HeadList.ListNode detectCycle(HeadList.ListNode head){ |
| 81 | + Map<HeadList.ListNode,Integer> map = new HashMap<>(); |
| 82 | + HeadList.ListNode tmp = head; |
| 83 | + int pos = 0; |
| 84 | + while (tmp != null){ //JC: tmp.next == null |
| 85 | + if (map.get(tmp) == null){ //JC: map.get() return something |
| 86 | + map.put(tmp,pos); |
| 87 | + tmp = tmp.next; |
| 88 | + pos++; |
| 89 | + } |
| 90 | + else break; |
| 91 | + } |
| 92 | + return tmp; |
| 93 | + } |
| 94 | + static HeadList.ListNode reverseKGroup(HeadList.ListNode head, int k){ |
| 95 | + if(k == 1 || k == 0) return head; |
| 96 | + HeadList.ListNode detect, pre, segPre, segTmp, tail, tailNext; |
| 97 | + if (head.next == null) return head; |
| 98 | + segPre = head; |
| 99 | + detect = pre = segTmp = head.next; |
| 100 | + if (head.next.next == null) return head; |
| 101 | + tail = pre.next; |
| 102 | + tailNext = tail.next; |
| 103 | + while (true){ |
| 104 | + int i = 0; |
| 105 | + for ( ; i < k; i++){ |
| 106 | + if (detect == null){ |
| 107 | + segPre.next = tailNext; |
| 108 | + return head; |
| 109 | + } |
| 110 | + else detect = detect.next; |
| 111 | + } |
| 112 | + for (int j = 0; j < k - 1; j++){ //K-1:N elements need N reverses. |
| 113 | + tail.next = pre; |
| 114 | + pre = tail; |
| 115 | + |
| 116 | + tail = tailNext; |
| 117 | + if (tailNext != null) tailNext = tailNext.next; |
| 118 | + |
| 119 | + } |
| 120 | + segPre.next = pre; |
| 121 | + pre = tail; |
| 122 | + tail = tailNext; |
| 123 | + if (tailNext != null) tailNext = tailNext.next; |
| 124 | + segPre = segTmp; |
| 125 | + segTmp = pre; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +/* Here is Supporting class |
| 132 | + Name: HeadList |
| 133 | + */ |
| 134 | +class HeadList{ |
| 135 | + static class ListNode{ |
| 136 | + int val; |
| 137 | + ListNode next; |
| 138 | + ListNode(int x){ |
| 139 | + val = x; |
| 140 | + } |
| 141 | + } |
| 142 | + private ListNode head; |
| 143 | + |
| 144 | + HeadList() { |
| 145 | + head = new ListNode(Integer.MIN_VALUE); |
| 146 | + } |
| 147 | + |
| 148 | + ListNode getHead(){ |
| 149 | + return this.head; |
| 150 | + } |
| 151 | + static boolean delete(ListNode head,ListNode node){ |
| 152 | + if (head == null || node == null) return false; |
| 153 | + ListNode tmp = head; |
| 154 | + while (tmp.next != node && tmp.next != null){ //JC: tmp.next == node || tmp.next == null |
| 155 | + tmp = tmp.next; |
| 156 | + } |
| 157 | + if (tmp.next == node){ |
| 158 | + tmp.next = node.next; |
| 159 | + return true; |
| 160 | + } |
| 161 | + else return false; |
| 162 | + } |
| 163 | + void insert(int x){ |
| 164 | + ListNode tmp = new ListNode(x); |
| 165 | + ListNode tail = head; |
| 166 | + while (tail.next !=null){ |
| 167 | + tail = tail.next; |
| 168 | + } |
| 169 | + tail.next = tmp; |
| 170 | + } |
| 171 | + static ListNode createCycle(int pos, HeadList list){ |
| 172 | + ListNode tail = list.head; |
| 173 | + while (tail.next !=null){ //get tail node. |
| 174 | + tail = tail.next; |
| 175 | + } |
| 176 | + ListNode pre = list.head; |
| 177 | + while (true){ |
| 178 | + for (int i = 0; i < pos+1; i++){ //because of the head node. |
| 179 | + pre = pre.next; |
| 180 | + } |
| 181 | + break; |
| 182 | + } |
| 183 | + tail.next = pre; |
| 184 | + return list.head; |
| 185 | + } |
| 186 | + static int searchNode(ListNode node, HeadList list){ |
| 187 | + ListNode tmp = list.head; |
| 188 | + int index = 0; |
| 189 | + while (node != tmp){ //JC: node == tmp |
| 190 | + if (tmp != null){ |
| 191 | + tmp = tmp.next; |
| 192 | + index++; |
| 193 | + } |
| 194 | + else return -1; |
| 195 | + } |
| 196 | + return index - 1; |
| 197 | + } |
| 198 | + |
| 199 | + |
| 200 | +} |
| 201 | + |
| 202 | + |
| 203 | +/* Here is Interface class |
| 204 | + Name: Output Input |
| 205 | + */ |
| 206 | +class Output{ |
| 207 | + static void printf(HeadList.ListNode head){ |
| 208 | + HeadList.ListNode tmp = head; |
| 209 | + while (tmp.next != null){ |
| 210 | + System.out.print(tmp.next.val); |
| 211 | + tmp = tmp.next; |
| 212 | + |
| 213 | + } |
| 214 | + } |
| 215 | + static void printf(int val){ |
| 216 | + System.out.print(val); |
| 217 | + } |
| 218 | +} |
| 219 | +class Input{ |
| 220 | + static HeadList getHeadList(){ |
| 221 | + Scanner sc = new Scanner(System.in); |
| 222 | + int length = sc.nextInt(); |
| 223 | + int[] array = new int[length]; |
| 224 | + for (int i = 0; i < array.length; i++){ |
| 225 | + array[i] = sc.nextInt(); |
| 226 | + } |
| 227 | + HeadList list = new HeadList(); |
| 228 | + for (int i : array){ |
| 229 | + list.insert(i); |
| 230 | + } |
| 231 | + return list; |
| 232 | + } |
| 233 | + static int getPositionOrK(){ |
| 234 | + Scanner sc = new Scanner(System.in); |
| 235 | + return sc.nextInt(); |
| 236 | + } |
| 237 | +} |
| 238 | + |
0 commit comments