package top100.top链表;
import top100.ListNode;
import top100.Node;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
public class TOP {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode cur1 = headA, cur2 = headB;
while (cur1 != cur2) {
cur1 = cur1 == null ? headB : cur1.next;
cur2 = cur2 == null ? headA : cur2.next;
}
return cur1;
}
public ListNode reverseList(ListNode head) {
if (head == null) return null;
ListNode pre = null, cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
public boolean isPalindrome(ListNode head) {
if (head == null) {
return true;
}
ArrayList<Integer> list = new ArrayList<>();
while (head != null) {
list.add(head.val);
head = head.next;
}
for (int i = 0; i < list.size() / 2; i++) {
int a = list.get(i);
int b = list.get(list.size() - 1 - i);
if (a != b) {
return false;
}
}
return true;
}
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) return false;
ListNode pre = null;
ListNode slow = head, fast = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
ListNode tmp = slow.next;
slow.next = pre;
pre = slow;
slow = tmp;
}
if(fast != null) slow = slow.next;
while (pre != null || slow != null){
if(pre.val != slow.val) return false;
pre = pre.next;
slow = slow.next;
}
return true;
}
public boolean hasCycle(ListNode head) {
if (head == null) {
return false;
}
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return true;
}
}
return false;
}
public ListNode detectCycle(ListNode head) {
if (head == null) {
return null;
}
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
fast = head;
while (fast != slow) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
return null;
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) {
return list2;
}
if (list2 == null) {
return list1;
}
ListNode temp = new ListNode(-1);
ListNode cur = temp;
while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
cur.next = list1;
list1 = list1.next;
} else {
cur.next = list2;
list2 = list2.next;
}
cur = cur.next;
}
if (list1 != null) {
cur.next = list1;
}
if (list2 != null) {
cur.next = list2;
}
return temp.next;
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;
ListNode fake = new ListNode(-1);
ListNode pre = fake;
while(l1 != null || l2 != null || carry != 0) {
int num1 = l1 == null ? 0 : l1.val;
int num2 = l2 == null ? 0 : l2.val;
ListNode cur = new ListNode((num1 + num2 + carry) % 10);
pre.next = cur;
pre = cur;
carry = (num1 + num2 + carry) / 10;
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
}
pre.next = null;
return fake.next;
}
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow = head;
ListNode fast = head;
for (int i = 0; i < n; i++) {
fast = fast.next;
}
ListNode pre = dummy;
while (fast != null) {
pre = slow;
slow = slow.next;
fast = fast.next;
}
pre.next = slow.next;
return dummy.next;
}
public ListNode swapPairs(ListNode head) {
if (head == null) return null;
if (head.next == null) return head;
ListNode dummy = head.next;
ListNode next = dummy.next;
dummy.next = head;
head.next = swapPairs(next);
return dummy;
}
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null) return null;
ListNode cur = head;
for (int i = 0; i < k; i++) {
if (cur == null) {
return head;
}
cur = cur.next;
}
ListNode pre = null;
cur = head;
int count = 1;
while (count <= k) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
count++;
}
head.next = reverseKGroup(cur, k);
return pre;
}
public Node copyRandomList(Node head) {
if (head == null) {
return null;
}
HashMap<Node, Node> map = new HashMap<>();
Node cur = head;
while (cur != null) {
map.put(cur, new Node(cur.val));
cur = cur.next;
}
cur = head;
while (cur != null) {
map.get(cur).next = map.get(cur.next);
map.get(cur).random = map.get(cur.random);
cur = cur.next;
}
return map.get(head);
}
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode slow = head, fast = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode temp = slow.next;
slow.next = null;
ListNode left = sortList(head);
ListNode right = sortList(temp);
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (left != null && right != null) {
if (left.val < right.val) {
cur.next = left;
left = left.next;
} else {
cur.next = right;
right = right.next;
}
cur = cur.next;
}
cur.next = left != null ? left : right;
return dummy.next;
}
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0) return null;
return mergeKLists(lists, 0, lists.length - 1);
}
private ListNode mergeKLists(ListNode[] lists, int left, int right) {
if (left == right) return lists[left];
int mid = left + (right - left) / 2;
ListNode node1 = mergeKLists(lists, left, mid);
ListNode node2 = mergeKLists(lists, mid + 1, right);
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (node1 != null && node2 != null) {
if (node1.val < node2.val) {
cur.next = node1;
node1 = node1.next;
} else {
cur.next = node2;
node2 = node2.next;
}
cur = cur.next;
}
cur.next = node1 == null ? node2 : node1;
return dummy.next;
}
class LRUCache {
int capacity;
HashMap<Integer, Integer> map;
ArrayDeque<Integer> deque;
public LRUCache(int capacity) {
this.capacity = capacity;
this.map = new HashMap<>();
this.deque = new ArrayDeque<>();
}
public int get(int key) {
if (map.containsKey(key)) {
deque.remove(key);
deque.offerFirst(key);
return map.get(key);
} else {
return -1;
}
}
public void put(int key, int value) {
if (map.containsKey(key)) {
map.put(key, value);
deque.remove(key);
} else {
if (map.size() == capacity) {
map.remove(deque.pollLast());
}
map.put(key, value);
}
deque.offerFirst(key);
}
}
}

- 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
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372