Skip to content

Commit

Permalink
feat: add csharp solution to lc problem: No.0019 (#4036)
Browse files Browse the repository at this point in the history
  • Loading branch information
djvelimir authored Feb 6, 2025
1 parent dee028e commit 3ca7f17
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
34 changes: 32 additions & 2 deletions solution/0000-0099/0019.Remove Nth Node From End of List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ var removeNthFromEnd = function (head, n) {

#### Swift

````swift
```swift
/**
* Definition for singly-linked list.
* public class ListNode {
Expand Down Expand Up @@ -340,6 +340,37 @@ def remove_nth_from_end(head, n)
end
```

#### C#

```cs
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode RemoveNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode fast = dummy, slow = dummy;
while (n-- > 0) {
fast = fast.next;
}
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
```

#### PHP

```php
Expand Down Expand Up @@ -381,4 +412,3 @@ class Solution {
<!-- solution:end -->

<!-- problem:end -->
````
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ var removeNthFromEnd = function (head, n) {

#### Swift

````swift
```swift
/**
* Definition for singly-linked list.
* public class ListNode {
Expand Down Expand Up @@ -337,6 +337,37 @@ def remove_nth_from_end(head, n)
end
```

#### C#

```cs
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode RemoveNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode fast = dummy, slow = dummy;
while (n-- > 0) {
fast = fast.next;
}
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
```

#### PHP

```php
Expand Down Expand Up @@ -378,4 +409,3 @@ class Solution {
<!-- solution:end -->

<!-- problem:end -->
````
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode RemoveNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode fast = dummy, slow = dummy;
while (n-- > 0) {
fast = fast.next;
}
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}

0 comments on commit 3ca7f17

Please sign in to comment.