-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwappingNodesinaLinkedList.java
42 lines (30 loc) · 1.03 KB
/
SwappingNodesinaLinkedList.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
package linkedlist;
import utils.LeetCode;
import utils.Level;
@LeetCode(no =1721 ,
level = Level.MEDIUM ,
title = "Swapping Nodes in a Linked List",
url="https://leetcode.com/problems/swapping-nodes-in-a-linked-list/")
public class SwappingNodesinaLinkedList {
public ListNode swapNodes(ListNode head, int k) {
ListNode temp=head;
int len=0;
while(temp!=null){
len++;
temp=temp.next;
}
ListNode nthFromBegin = head;
ListNode nthFromLast=head;
// 2) get the (len-n+1)th node from the beginning
for (int i = 1; i < len - k + 1; i++)
nthFromLast = nthFromLast.next;
System.out.println("nthFromLast "+nthFromLast.val);
for (int i = 1; i <=k-1; i++)
nthFromBegin = nthFromBegin.next;
System.out.println("nthFromBegin "+nthFromBegin.val);
int tempVal= nthFromBegin.val;
nthFromBegin.val=nthFromLast.val;
nthFromLast.val=tempVal;
return head;
}
}