File tree 3 files changed +90
-4
lines changed
solution/0000-0099/0019.Remove Nth Node From End of List
3 files changed +90
-4
lines changed Original file line number Diff line number Diff line change @@ -280,7 +280,7 @@ var removeNthFromEnd = function (head, n) {
280
280
281
281
#### Swift
282
282
283
- ```` swift
283
+ ``` swift
284
284
/**
285
285
* Definition for singly-linked list.
286
286
* public class ListNode {
@@ -340,6 +340,37 @@ def remove_nth_from_end(head, n)
340
340
end
341
341
```
342
342
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
+
343
374
#### PHP
344
375
345
376
``` php
@@ -381,4 +412,3 @@ class Solution {
381
412
<!-- solution: end -->
382
413
383
414
<!-- problem: end -->
384
- ````
Original file line number Diff line number Diff line change @@ -277,7 +277,7 @@ var removeNthFromEnd = function (head, n) {
277
277
278
278
#### Swift
279
279
280
- ```` swift
280
+ ``` swift
281
281
/**
282
282
* Definition for singly-linked list.
283
283
* public class ListNode {
@@ -337,6 +337,37 @@ def remove_nth_from_end(head, n)
337
337
end
338
338
```
339
339
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
+
340
371
#### PHP
341
372
342
373
``` php
@@ -378,4 +409,3 @@ class Solution {
378
409
<!-- solution: end -->
379
410
380
411
<!-- problem: end -->
381
- ````
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments