Guest User

Untitled

a guest
Dec 10th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var list = new LinkedList<int>();
  6. list.AddLast(2);
  7. list.AddLast(1);
  8. list.AddLast(1);
  9. list.AddLast(2);
  10.  
  11. Console.WriteLine(IsPalindrome(list));
  12. }
  13.  
  14. static bool IsPalindrome(LinkedList<int> list)
  15. {
  16. if (list.Count == 0)
  17. return false;
  18.  
  19. if (list.Count == 1)
  20. return true;
  21.  
  22. var halfList = list.Count / 2;
  23. var current = list.First;
  24. var stack = new Stack<int>();
  25.  
  26. while (halfList > 0)
  27. {
  28. stack.Push(current.Value);
  29. current = current.Next;
  30. halfList--;
  31. }
  32.  
  33. var isOdd = (list.Count % 2) != 0;
  34. if (isOdd)
  35. current = current.Next;
  36.  
  37. while(stack.Count != 0)
  38. {
  39. if (stack.Pop() != current.Value)
  40. return false;
  41.  
  42. current = current.Next;
  43. }
  44.  
  45. return true;
  46. }
  47. }
Add Comment
Please, Sign In to add comment