Skip to content

Instantly share code, notes, and snippets.

@geoff-m
Created August 6, 2020 20:15
Show Gist options
  • Save geoff-m/1b60de2a087fa0c2c68734fc7716bb21 to your computer and use it in GitHub Desktop.
Save geoff-m/1b60de2a087fa0c2c68734fc7716bb21 to your computer and use it in GitHub Desktop.
Test whether a number is even using a linked list
class Utility {
class LinkedListNode<T>
{
public T Value;
public LinkedListNode<T> Next;
}
static bool IsEven(int n)
{
// Create a circular linked list with 2 nodes.
var even = new LinkedListNode<bool>() { Value = true };
var odd = new LinkedListNode<bool>() { Value = false };
even.Next = odd;
odd.Next = even;
var current = even;
if (n < 0)
n = -n;
while (n > 0)
{
--n;
current = current.Next;
}
return current.Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment