Delete the Middle Node of a Linked List
删掉中间的node.
/**
* 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;
if(head.next.next == null){
head.next = null;
return head;
}
ListNode dhead = new ListNode();
dhead.next = head;
ListNode slow = new ListNode();
slow.next = head;
ListNode fast = new ListNode();
fast.next = head;
while(fast.next != null && fast.next.next != null)
{
slow = slow.next;
fast = fast.next.next;
}
slow.next = slow.next.next;
return dhead.next;
}
}