Skip to content

Commit 3ca7f17

Browse files
authored
feat: add csharp solution to lc problem: No.0019 (#4036)
1 parent dee028e commit 3ca7f17

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

solution/0000-0099/0019.Remove Nth Node From End of List/README.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ var removeNthFromEnd = function (head, n) {
280280

281281
#### Swift
282282

283-
````swift
283+
```swift
284284
/**
285285
* Definition for singly-linked list.
286286
* public class ListNode {
@@ -340,6 +340,37 @@ def remove_nth_from_end(head, n)
340340
end
341341
```
342342

343+
#### C#
344+
345+
```cs
346+
/**
347+
* Definition for singly-linked list.
348+
* public class ListNode {
349+
* public int val;
350+
* public ListNode next;
351+
* public ListNode(int val=0, ListNode next=null) {
352+
* this.val = val;
353+
* this.next = next;
354+
* }
355+
* }
356+
*/
357+
public class Solution {
358+
public ListNode RemoveNthFromEnd(ListNode head, int n) {
359+
ListNode dummy = new ListNode(0, head);
360+
ListNode fast = dummy, slow = dummy;
361+
while (n-- > 0) {
362+
fast = fast.next;
363+
}
364+
while (fast.next != null) {
365+
slow = slow.next;
366+
fast = fast.next;
367+
}
368+
slow.next = slow.next.next;
369+
return dummy.next;
370+
}
371+
}
372+
```
373+
343374
#### PHP
344375

345376
```php
@@ -381,4 +412,3 @@ class Solution {
381412
<!-- solution:end -->
382413

383414
<!-- problem:end -->
384-
````

solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ var removeNthFromEnd = function (head, n) {
277277

278278
#### Swift
279279

280-
````swift
280+
```swift
281281
/**
282282
* Definition for singly-linked list.
283283
* public class ListNode {
@@ -337,6 +337,37 @@ def remove_nth_from_end(head, n)
337337
end
338338
```
339339

340+
#### C#
341+
342+
```cs
343+
/**
344+
* Definition for singly-linked list.
345+
* public class ListNode {
346+
* public int val;
347+
* public ListNode next;
348+
* public ListNode(int val=0, ListNode next=null) {
349+
* this.val = val;
350+
* this.next = next;
351+
* }
352+
* }
353+
*/
354+
public class Solution {
355+
public ListNode RemoveNthFromEnd(ListNode head, int n) {
356+
ListNode dummy = new ListNode(0, head);
357+
ListNode fast = dummy, slow = dummy;
358+
while (n-- > 0) {
359+
fast = fast.next;
360+
}
361+
while (fast.next != null) {
362+
slow = slow.next;
363+
fast = fast.next;
364+
}
365+
slow.next = slow.next.next;
366+
return dummy.next;
367+
}
368+
}
369+
```
370+
340371
#### PHP
341372

342373
```php
@@ -378,4 +409,3 @@ class Solution {
378409
<!-- solution:end -->
379410

380411
<!-- problem:end -->
381-
````
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode RemoveNthFromEnd(ListNode head, int n) {
14+
ListNode dummy = new ListNode(0, head);
15+
ListNode fast = dummy, slow = dummy;
16+
while (n-- > 0) {
17+
fast = fast.next;
18+
}
19+
while (fast.next != null) {
20+
slow = slow.next;
21+
fast = fast.next;
22+
}
23+
slow.next = slow.next.next;
24+
return dummy.next;
25+
}
26+
}

0 commit comments

Comments
 (0)