-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete Kth Node From End.java
61 lines (53 loc) · 1.17 KB
/
Delete Kth Node From End.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
/****************************************************************
Following is the class structure of the Node class:
class Node {
public int data;
public Node next;
public Node prev;
Node()
{
this.data = 0;
this.next = null;
this.prev = null;
}
Node(int data)
{
this.data = data;
this.next = null;
this.prev = null;
}
Node(int data, Node next)
{
this.data = data;
this.next = next;
this.prev = next;
}
};
*****************************************************************/
public class Solution
{
public static Node removeKthNode(Node head, int K)
{
Node curr=head;
int count=0;
while(curr!=null){
count++;
curr=curr.next;
}
curr=head;
Node prev=null;
if (count==K){
head=head.next;
return head;
}
while(count!=K){
prev=curr;
curr=curr.next;
count--;
}
prev.next=curr.next;
curr.next=null;
return head;
// Write your code here.
}
}