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
| class Solution { public ListNode deleteMiddle(ListNode head) { int len=getLen(head); if(len==0) return null; float half=len/2.0f; if((len&1)==1){ deleteNode(head,(int)Math.ceil(half)); }else{ deleteNode(head,(int)half); } return head; } public int getLen(ListNode head) { ListNode temp=head; int i=0; while(temp.next!=null){ temp=temp.next; i++; } return i; } public void deleteNode(ListNode head,int n) { ListNode temp=head; int i=0; while(temp.next!=null){ if(i==(n-1))break; temp=temp.next; i++; } temp.next=temp.next.next; } }
|