Skip to content

Instantly share code, notes, and snippets.

public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; next = null; }
}
public bool HasCycle(ListNode head)
{
if (head == null)
public int RomanToInt(string s)
{
int result = 0;
int count = s.Count();
char setChar = '\b';
for (int i = count - 1; i >= 0; i--)
{
switch (s[i])
{
case 'I':
public int RomanToInt(string s)
{
int result = 0;
switch (s)
{
case "I":
result += 1;
break;
case "V":
result += 5;
[TestClass]
public class No206Test_ReverseLinkedList
{
private No206_ReverseLinkedList solution;
[TestInitialize]
public void Init()
{
solution = new No206_ReverseLinkedList();
}
public ListNode ReverseList(ListNode head)
{
if (head == null || head.next == null)
return head;
ListNode tail = ReverseList(head.next);
head.next.next = head;
// 無這行對中間的node無影響,但需指定這行才可使原本的head最終指向null
head.next = null;
return tail;
}
public ListNode ReverseList(ListNode head)
{
if (head == null || head.next == null)
return head;
ListNode curr = head;
ListNode pre = null;
ListNode next = curr.next;
head.next = null;
while(next.next != null)
[TestClass]
public class No206Test_ReverseLinkedList
{
private No206_ReverseLinkedList solution;
[TestInitialize]
public void Init()
{
solution = new No206_ReverseLinkedList();
}
[TestMethod]
public void ArrayContain2SameHugeNumber_ReturnTrue()
{
int[] array = new int[2] { -1200000005, -1200000005 };
bool isDuplicate = solution.ContainsDuplicate(array);
Assert.IsTrue(isDuplicate);
}
[TestMethod]
public void Array2_14_18_22_22_ReturnTrue()
public bool ContainsDuplicate(int[] nums)
{
int distinctNums = nums.Distinct().Count();
int originalNums = nums.Count();
return distinctNums != originalNums;
}
[TestMethod]
public void ArrayContain0andNagativeNoDuplicate_ReturnFalse()
{
int[] array = new int[5] { 1, 5, -2, -4, 0 };
bool isDuplicate = solution.ContainsDuplicate(array);
Assert.IsFalse(isDuplicate);
}