-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 3.java
41 lines (39 loc) · 965 Bytes
/
Day 3.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
// LEETCODE : 19. Remove Nth Node From End of List
/**
* 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; }
* }
*/
class Day3 {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode curr=head;
int count = 0;
if (curr.next==null){
count++;
}
while (curr.next!=null){
curr=curr.next;
count++;
if (curr.next==null){
count++;
break;
}
}int l = count-n;
int x = 0;
curr = head;
if (count==n){
return head.next;
}
while (x!=l-1){
curr = curr.next;
x++;
}ListNode L=curr;
curr.next = L.next.next;
return head;
}
}