Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add csharp solution to lc problem: No.0019 #4036

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}