-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1721.swapping-nodes-in-a-linked-list.java
executable file
·50 lines (39 loc) · 1.28 KB
/
1721.swapping-nodes-in-a-linked-list.java
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
package Java;
import java.util.ArrayList;
import java.util.LinkedList;
class SwappingNodesInALinkedList{
public static void main(String[] args) {
Solution solution = new SwappingNodesInALinkedList().new Solution();
}
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public ListNode swapNodes(ListNode head, int k) {
if(head.next == null){
return head;
}
LinkedList<ListNode> list = new LinkedList();
byte iterations = 0;
ListNode firstK = new ListNode();
ArrayList<ListNode> balls = new ArrayList<>();
ListNode temp = head;
while (temp.next != null) {
balls.add(temp);
list.add(temp);
temp = temp.next;
};
int length = balls.size();
int loink = list.get(k).val;
list.get(k-1).val = list.get(length-k).val;
list.get(length-k).val = loink;
return head;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}