Skip to content

Commit 0b60257

Browse files
authored
Create 19. Remove Nth Node From End of List
1 parent 49a80f8 commit 0b60257

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: 19. Remove Nth Node From End of List

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode removeNthFromEnd(ListNode head, int n) {
13+
ListNode dummy =new ListNode(0);
14+
dummy.next = head;
15+
ListNode current = dummy, nth = dummy;
16+
17+
for(int i=1;i<=n+1;i++){
18+
current = current.next;
19+
}
20+
21+
while(current!=null){
22+
current = current.next;
23+
nth = nth.next;
24+
}
25+
26+
nth.next = nth.next.next;
27+
28+
return dummy.next;
29+
}
30+
}

0 commit comments

Comments
 (0)