Note:

头会变,所以需要dummyHead

需要curIndex来存当前结点

遇到删除都需要prev和next

Key word:

在多个indices处删除ListNodes

public class Solution {
  public ListNode deleteNodes(ListNode head, int[] indices) {
    // corner case
    if (head == null) {
      return null;
    }
    ListNode dummyHead = new ListNode(0);
    ListNode prev = dummyHead;
    ListNode cur = head;
    prev.next = cur;
    int curIndex = 0;
    int index = 0;
    while (cur != null) {
      ListNode next = cur.next;
      if (index < indices.length && curIndex == indices[index]) {
        prev.next = cur.next;
        index++;
      } else {
        prev = cur;
      }
      curIndex++;
      cur = next;
    }
    return dummyHead.next;
  }
}

Complexity Analysis:

TC: O(n);

SC: O(1);