Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- var list = new LinkedList<int>();
- list.AddLast(2);
- list.AddLast(1);
- list.AddLast(1);
- list.AddLast(2);
- Console.WriteLine(IsPalindrome(list));
- }
- static bool IsPalindrome(LinkedList<int> list)
- {
- if (list.Count == 0)
- return false;
- if (list.Count == 1)
- return true;
- var halfList = list.Count / 2;
- var current = list.First;
- var stack = new Stack<int>();
- while (halfList > 0)
- {
- stack.Push(current.Value);
- current = current.Next;
- halfList--;
- }
- var isOdd = (list.Count % 2) != 0;
- if (isOdd)
- current = current.Next;
- while(stack.Count != 0)
- {
- if (stack.Pop() != current.Value)
- return false;
- current = current.Next;
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment