输入链表头节点,奇数长度返回中点,偶数长度返回上终点
/**
* 输入链表头节点,奇数长度返回中点,偶数长度返回上中点
* @param head
* @return
*/
public ListNode midOrUpMid(ListNode head) {
if (head == null || head.next == null || head.next.next == null) {
return head;
}
ListNode slow = head.next;
ListNode fast = head.next.next;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
输入链表头节点,奇数长度返回中点,偶数长度返回下终点
/**
* 输入链表头节点,奇数长度返回中点,偶数长度返回下中点
* @param head
* @return
*/
public ListNode midOrDownMid(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode slow = head.next;
ListNode fast = head.next;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
输入链表头节点,奇数长度返回中点的前一个,偶数长度返回上中点前一个
/**
* 输入链表头节点,奇数长度返回中点的前一个,偶数长度返回上中点前一个
* @param head
* @return
*/
public ListNode preMidOrUpPreMid(ListNode head){
if(head == null || head.next==null || head.next.next==null){
return null;
}
ListNode slow = head;
ListNode fast = head.next.next;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
输入链表头节点,奇数长度返回中点的前一个,偶数长度返回下中点前一个
/**
* 输入链表头节点,奇数长度返回中点的前一个,偶数长度返回下中点前一个
* @param head
* @return
*/
public ListNode preMidOrDownPreMid(ListNode head){
if(head == null || head.next==null){
return null;
}
ListNode slow = head;
ListNode fast = head.next;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}