-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelete Kth node from End.java
More file actions
38 lines (33 loc) · 1001 Bytes
/
Delete Kth node from End.java
File metadata and controls
38 lines (33 loc) · 1001 Bytes
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
import java.util.* ;
import java.io.*;
/****************************************************************
Following is the structure of the Singly Linked List.
class LinkedListNode<T> {
T data;
LinkedListNode<T> next;
public LinkedListNode(T data) {
this.data = data;
}
}
****************************************************************/
public class Solution {
public static LinkedListNode<Integer> removeKthNode(LinkedListNode<Integer> head, int K) {
// Write your code here.
LinkedListNode<Integer> dummy = new LinkedListNode(0);
dummy.next = head;
LinkedListNode<Integer> fast = dummy;
LinkedListNode<Integer> slow = dummy;
for(int i=0;i<K;i++){
fast = fast.next;
}
while(fast.next!=null){
slow = slow.next;
fast = fast.next;
}
if(K==0){
return dummy.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}