Day4
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_876._Middle_of_the_Linked_List</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
37
876. Middle of the Linked List/ListNode.cs
Normal file
37
876. Middle of the Linked List/ListNode.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
namespace _876._Middle_of_the_Linked_List;
|
||||
|
||||
//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 static ListNode Create(int[] nums)
|
||||
{
|
||||
ListNode l = new ListNode(nums[0]);
|
||||
ListNode cur = l;
|
||||
for (int i = 1; i < nums.Length; i++)
|
||||
{
|
||||
cur.next = new ListNode(nums[i]);
|
||||
cur = cur.next;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
List<int> list = new List<int>();
|
||||
ListNode? cur = this;
|
||||
while (cur != null)
|
||||
{
|
||||
list.Add(cur.val);
|
||||
cur = cur.next;
|
||||
}
|
||||
return String.Join(" ", list);
|
||||
}
|
||||
}
|
||||
13
876. Middle of the Linked List/Program.cs
Normal file
13
876. Middle of the Linked List/Program.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace _876._Middle_of_the_Linked_List
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var l1 = ListNode.Create(new int[] { 1,2,3,4,5});
|
||||
var l2 = ListNode.Create(new int[] { 1,2,3,4,5,6});
|
||||
Console.WriteLine(new Solution().MiddleNode(l1).val);
|
||||
Console.WriteLine(new Solution().MiddleNode(l2).val);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
876. Middle of the Linked List/Solution.cs
Normal file
15
876. Middle of the Linked List/Solution.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace _876._Middle_of_the_Linked_List;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public ListNode MiddleNode(ListNode? head)
|
||||
{
|
||||
ListNode result = head!;
|
||||
while(head?.next != null)
|
||||
{
|
||||
head = head?.next?.next;
|
||||
result = result!.next!;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user