public static class Node{
public int val;
public Node next;
Node(int val){
this.val = val;
}
}
//返回中点或上中点
public static Node midOrUpMidNode(Node head){
if (head == null||head.next==null||head.next.next==null){
return head;
}
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;
}
//返回 中点或上中点 的前一个
public static Node midOrUpMidpreNode(Node head){
if (head == null||head.next==null||head.next.next==null){
return null;
}
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;
}
06-09
476