代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteMiddle(ListNode head) {
if(head==null||head.next==null)return null;
ListNode cur = head;
ListNode cur2 = head;
ListNode last = cur;
while(cur2.next!=null&&cur2.next.next!=null){
cur2 = cur2.next.next;
last = cur;
cur = cur.next;
}
if(cur2.next==null){
last.next = last.next.next;
}else{
cur.next = cur.next.next;
}
return head;
}
}