Submission Detail

132 / 132 test cases passed.
Status:

Accepted

Runtime: 4 ms
Memory Usage: 57.5 MB
Submitted: 5 days, 1 hour ago
loading
Runtime Error Message:
Last executed input:
Input:
Output:
Expected:

Accepted Solutions Runtime Distribution

0
2
4
6
8
10
12
14
16
18
20
22
10
20
30
40
50
60
70
java
You are here!
Your runtime beats 19.35 % of java submissions.
Runtime (ms)
Distribution (%)

0.0
2.5
5.0
7.5
10.0
12.5
15.0
17.5
20.0
25
50
Zoom area by dragging across this chart

Accepted Solutions Memory Distribution

55500
52500
53000
53500
54000
54500
55000
56000
56500
57000
57500
58000
58500
5
10
15
java
You are here!
Your memory usage beats 42.95 % of java submissions.
Memory (KB)
Distribution (%)

55500
52500
53000
53500
54000
54500
55000
56000
56500
57000
57500
58000
58500
5
10
15
Zoom area by dragging across this chart

Invite friends to challenge Swapping Nodes in a Linked List


Submitted Code: 5 days, 1 hour ago

Language: 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
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Definition for singly-linked list.
* 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; }
* }
*/
class Solution {
public ListNode swapNodes(ListNode head, int k) {
int size = 0;
ListNode n = head;
// determine size of list
while (n != null) {
size++;
n = n.next;
}
// if only 2 elements, swap them:
if (size == 2) {
int temp = head.val;
head.val = head.next.val;
head.next.val = temp;
return head;
}
ListNode n1 = head;
int count = 1;
// travel to node k from head
while (count < k) {
n1 = n1.next;
count++;
}
// find second node
ListNode n2 = head;
count = 0;
while (count < size - k) {
n2 = n2.next;
count++;
}
// swap nodes
int temp = n1.val;
n1.val = n2.val;
n2.val = temp;
// print new list
ListNode i = new ListNode();
i = head;
while (i != null) {
i = i.next;
}
return head;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX