Created
August 6, 2020 20:15
-
-
Save geoff-m/1b60de2a087fa0c2c68734fc7716bb21 to your computer and use it in GitHub Desktop.
Test whether a number is even using a linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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