Linked List Delete Middle Node
Solution
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode DeleteMiddle(ListNode head) {
if (head.next == null) return null;
ListNode fast = head;
ListNode slow = new ListNode(0, head);
while (fast != null && fast.next != null)
{
fast = fast.next.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
}Last updated