1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
class Solution { public ListNode middleNode(ListNode head) { if(head==null||head.next==null) return head; int len=0; ListNode middle=head; while(head!=null){ len++; head=head.next; } for(int i=1;i<Math.round(len/2)+1;i++){ middle=middle.next; } return middle; } public ListNode middleNode(ListNode head) { if(head==null||head.next==null) return head; if(head.next.next==null) return head.next; ListNode slowy=head,quick=head; while(quick!=null&&quick.next!=null){ slowy=slowy.next; quick=quick.next.next; } return slowy; }
public ListNode middleNode(ListNode head) { if(head==null||head.next==null) return head; if(head.next.next==null) return head.next; ListNode quick=head; while(quick!=null&&quick.next!=null){ head=head.next; quick=quick.next.next; } return head; } }
|