-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
40 lines (39 loc) · 1.02 KB
/
Copy pathsolution.java
File metadata and controls
40 lines (39 loc) · 1.02 KB
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
// 19. Remove Nth Node From End of List
// https://leetcode.com/problems/remove-nth-node-from-end-of-list/
// Medium | Java | Accepted 2025-11-05
// Runtime 0 ms | Memory 43.5 MB
/**
* 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 Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode start = dummy.next;
ListNode copy = head;
int count = 0;
while(copy!=null)
{
count++;
copy = copy.next;
}
int stop = count-n-1;
if(stop==-1)
{
return dummy.next.next;
}
for(int i = 0; i<stop; i++)
{
start = start.next;
}
start.next = start.next.next;
return dummy.next;
}
}