public static ListNode delete(ListNode head, int k) { if(head == null ) { return head; } if(k < 0 || k > length(head)) { return head; } if (k == 0){ head = head.getNext();//not able to delete the 0th index element (i.e. head) }else { int count = 0; ListNode temp = head; while(count < k-1) { temp = temp.getNext(); count++; } ListNode temporary = temp; temp = temp.getNext().getNext(); temporary.setNext(temp); } return head; }
Copy and insert this code into your website: