-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveNth.java
More file actions
36 lines (33 loc) · 850 Bytes
/
RemoveNth.java
File metadata and controls
36 lines (33 loc) · 850 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
public class RemoveNth {
public int Length(ListNode head){
int l=0;
ListNode temp = head;
while(temp!=null){
l++;
temp = temp.next;
}
System.out.println(l);
return l;
}
public ListNode removeNthFromEnd(ListNode head, int n) {
int len = Length(head);
if(len == 0)
return head;
ListNode temp = head;
if(len-n == 0){
head = temp.next;
return head;
}
else{
int c = 0;
ListNode prev =head;
while(c<(len-n)){
prev = temp;
temp = temp.next;
c++;
}
prev.next = temp.next;
}
return head;
}
}