Day4
This commit is contained in:
28
142. Linked List Cycle II/Solution.cs
Normal file
28
142. Linked List Cycle II/Solution.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace _142._Linked_List_Cycle_II;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public ListNode? DetectCycle(ListNode head)
|
||||
{
|
||||
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;
|
||||
}
|
||||
cur = head;
|
||||
curAbs = cur.val < 0 ? (cur.val * -1) : cur.val;
|
||||
while (cur != null && (curAbs & bit) != 0)
|
||||
{
|
||||
cur.val ^= bit;
|
||||
cur = cur.next;
|
||||
if (cur != null)
|
||||
curAbs = cur.val < 0 ? (cur.val * -1) : cur.val;
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user