Untitled
raw download clone
TEXT
views 14
,
size 656 b
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;
        }
close fullscreen
Login or Register to edit or fork this paste. It's free.