-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveNthNodeFromEndOfList.java
41 lines (40 loc) · 1.08 KB
/
RemoveNthNodeFromEndOfList.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; }
* }
*/
// 19.problems/remove-nth-node-from-end-of-list
class RemoveNthNodeFromEndOfList {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(n==1 && head.next == null){
return null;
}
int count = 0;
ListNode headNode = head;
while(headNode != null){
count = count + 1;
headNode = headNode.next;
}
count = count - n - 1;
if(count < 0){
return head.next;
}
headNode = head;
int count2=0;
while(headNode.next!=null){
if(count2 == count){
ListNode tempNode = headNode.next;
headNode.next = tempNode.next;
break;
}
headNode = headNode.next;
count2 = count2 + 1;
}
return head;
}
}