Skip to content

Commit 70e28b6

Browse files
add 1721
1 parent e901b74 commit 70e28b6

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1721|[Swapping Nodes in a Linked List](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1721.java) ||Medium|LinkedList|
1112
|1720|[Decode XORed Array](https://leetcode.com/problems/decode-xored-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1720.java) ||Easy|Bit Manipulation|
1213
|1718|[Construct the Lexicographically Largest Valid Sequence](https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1718.java) ||Medium|Backtracking, Recursion|
1314
|1716|[Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1716.java) ||Easy|Math, Greedy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class _1721 {
9+
public static class Solution1 {
10+
public ListNode swapNodes(ListNode head, int k) {
11+
List<Integer> list = new ArrayList<>();
12+
ListNode tmp = head;
13+
while (tmp != null) {
14+
list.add(tmp.val);
15+
tmp = tmp.next;
16+
}
17+
int first = list.get(k - 1);
18+
int size = list.size();
19+
int second = list.get(size - k);
20+
list.remove(k - 1);
21+
list.add(k - 1, second);
22+
list.remove(size - k);
23+
list.add(size - k, first);
24+
ListNode pre = new ListNode(-1);
25+
tmp = pre;
26+
for (int i = 0; i < list.size(); i++) {
27+
pre.next = new ListNode(list.get(i));
28+
pre = pre.next;
29+
}
30+
return tmp.next;
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)