How fast a pointer

  1. Enter the list head and return the midpoint for odd lengths and the upper midpoint for even lengths
// Enter the header of the list, return the midpoint of the odd length, Even the length of the return on midpoint public static Node midOrUpMidNode (head) {if (head = = null | | head. The next = = null | | head. Next. Next = = null) { return head; } // List with three or more nodes Node slow = head.next; Node fast = head.next.next; while (fast.next ! = null && fast.next.next ! = null) { slow = slow.next; fast = fast.next.next; } return slow; }Copy the code
  1. Enter the list head and return the midpoint for odd lengths and the lower midpoint for even lengths
// Enter the header of the list, return the midpoint of the odd length, Even length returns the midpoint public static Node midOrDownMidNode (head) {if (head = = null | | head. The next = = null) {return head; } // The list has two or more nodes //slow and fast both come to the position of the second Node. Node fast = head.next; while (fast.next ! = null && fast.next.next ! = null) { slow = slow.next; fast = fast.next.next; } return slow; }Copy the code
  1. Enter the list head and return the one before the midpoint for the odd length and the one before the upper midpoint for the even length
// Enter the header of the list, the odd length returns the one before the midpoint, Even length return point before a public static Node midOrUpMidPreNode (head) {if (head = = null | | head. The next = = null | | head. Next. The next == null) { return head; } // List with three or more nodes Node slow = head; Node fast = head.next.next; while (fast.next ! = null && fast.next.next ! = null) { slow = slow.next; fast = fast.next.next; } return slow; }Copy the code
  1. Enter the list head and return the one before the midpoint for the odd length and the one before the next midpoint for the even length
// Enter the header of the list, the odd length returns the one before the midpoint, Even length before returning to the halfway point of a public static Node midOrDownMidPreNode (head) {if (head = = null | | head. The next = = null) {return head; } // The list has two or more nodes //slow and fast both come to the position of the second Node. Node fast = head.next; while (fast.next ! = null && fast.next.next ! = null) { slow = slow.next; fast = fast.next.next; } return slow; }Copy the code

List palindrome

// If a single linked list has a head node (head), check whether the list is palindromic. Public static Boolean isPalindrome1(Node head) {Stack<Node> Stack = new Stack<>(); static Boolean isPalindrome1(Node head) {Stack<Node> Stack = new Stack<>(); Node cur = head; while (cur ! = null) { stack.push(cur); cur = cur.next; } cur = head; while (cur ! = null) { if (cur.data ! = stack.pop().data) { return false; } cur = cur.next; } return true; }Copy the code