-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
41 lines (36 loc) · 933 Bytes
/
Solution.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
/**
* 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) {
if (head == null)
return head;
ListNode l = head, d1 = head, d2 = head;
// double pointers;
int count = 1;
int ln, tmp;
while (count < k) {
d2 = d2.next;
count++;
}
l = d2;
ln = l.val;
// System.out.println(ln);
while (d2.next != null) {
d1 = d1.next;
d2 = d2.next;
}
// System.out.println(d1.val);
tmp = d1.val;
d1.val = ln;
l.val = tmp;
return head;
}
}