private static bool checkListPalindromicity(myList list) { Node slow = list.head, fast = list.head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } Node firstHalfHead = list.head; Node reversedSublistHead = reverseSublist(slow); while (reversedSublistHead != null && firstHalfHead != null) { if (reversedSublistHead.data != firstHalfHead.data) { return(false); } reversedSublistHead = reversedSublistHead.next; firstHalfHead = firstHalfHead.next; } return(true); }
static void Main(string[] args) { myList list = new myList(); list.append(1); list.append(2); list.append(5); list.append(0); list.append(5); list.append(2); list.append(1); bool palindrome = checkListPalindromicity(list); if (palindrome) { Console.WriteLine("Is palindrome"); } else { Console.WriteLine("Is not palindrome"); } }