Day7
This commit is contained in:
11
141. Linked List Cycle/141. Linked List Cycle.csproj
Normal file
11
141. Linked List Cycle/141. Linked List Cycle.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_141._Linked_List_Cycle</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
14
141. Linked List Cycle/ListNode.cs
Normal file
14
141. Linked List Cycle/ListNode.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace _141._Linked_List_Cycle;
|
||||
|
||||
//Definition for singly-linked list.
|
||||
public class ListNode
|
||||
{
|
||||
public int val;
|
||||
public ListNode? next;
|
||||
public ListNode(int x)
|
||||
{
|
||||
val = x;
|
||||
next = null;
|
||||
}
|
||||
}
|
||||
|
||||
9
141. Linked List Cycle/Program.cs
Normal file
9
141. Linked List Cycle/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _141._Linked_List_Cycle;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
23
141. Linked List Cycle/Solution.cs
Normal file
23
141. Linked List Cycle/Solution.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
namespace _141._Linked_List_Cycle;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public bool HasCycle(ListNode head)
|
||||
{
|
||||
if (head == null)
|
||||
return false;
|
||||
ListNode? cur = head;
|
||||
int bit = 1 << 28;
|
||||
int curAbs = 0;
|
||||
while (cur != null && (curAbs & bit) == 0)
|
||||
{
|
||||
cur.val ^= bit;
|
||||
cur = cur.next;
|
||||
if (cur != null)
|
||||
curAbs = cur.val < 0 ? (cur.val * -1) : cur.val;
|
||||
}
|
||||
if ((curAbs & bit) != 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user