mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
@ -412,6 +412,28 @@ struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
|
||||
|
||||
```
|
||||
|
||||
C#:
|
||||
```csharp
|
||||
public class Solution {
|
||||
public ListNode RemoveNthFromEnd(ListNode head, int n) {
|
||||
ListNode dummpHead = new ListNode(0);
|
||||
dummpHead.next = head;
|
||||
var fastNode = dummpHead;
|
||||
var slowNode = dummpHead;
|
||||
while(n-- != 0 && fastNode != null)
|
||||
{
|
||||
fastNode = fastNode.next;
|
||||
}
|
||||
while(fastNode.next != null)
|
||||
{
|
||||
fastNode = fastNode.next;
|
||||
slowNode = slowNode.next;
|
||||
}
|
||||
slowNode.next = slowNode.next.next;
|
||||
return dummpHead.next;
|
||||
}
|
||||
}
|
||||
```
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -437,6 +437,34 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
C#:
|
||||
```CSharp
|
||||
public class Solution
|
||||
{
|
||||
public ListNode DetectCycle(ListNode head)
|
||||
{
|
||||
ListNode fast = head;
|
||||
ListNode slow = head;
|
||||
while (fast != null && fast.next != null)
|
||||
{
|
||||
slow = slow.next;
|
||||
fast = fast.next.next;
|
||||
if (fast == slow)
|
||||
{
|
||||
fast = head;
|
||||
while (fast != slow)
|
||||
{
|
||||
fast = fast.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
return fast;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -596,6 +596,41 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
C#
|
||||
```CSharp
|
||||
/**
|
||||
* 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 RemoveElements(ListNode head, int val)
|
||||
{
|
||||
ListNode dummyHead = new ListNode(0,head);
|
||||
ListNode temp = dummyHead;
|
||||
while(temp.next != null)
|
||||
{
|
||||
if(temp.next.val == val)
|
||||
{
|
||||
temp.next = temp.next.next;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp = temp.next;
|
||||
}
|
||||
}
|
||||
return dummyHead.next;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -488,6 +488,14 @@ public class Solution {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
C# LINQ:
|
||||
```csharp
|
||||
public class Solution {
|
||||
public int[] SortedSquares(int[] nums) {
|
||||
return nums.Select(x => x * x).OrderBy(x => x).ToArray();
|
||||
}
|
||||
}
|
||||
```
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user