You are viewing a single comment's thread. Return to all comments →
Nice solutions! Here was my solution-
Node Delete(Node head, int position) { if (position == 0) { return head.next; } Node currentNode = head; Node prevNode = head; for (int i = 0; i < position; i++) { prevNode = currentNode; if (currentNode.next == null) { break; } currentNode = currentNode.next; } prevNode.next = currentNode.next; return head; }
Later I changed it to this-
Node Delete(Node head, int position) { if (position > 0 && head.next == null) { return head; } if (position == 0 || head.next == null) { return head.next; } head.next = Delete(head.next, position - 1); return head; }
Which can delete the last element as well as can ignore delete operation which position is > ListSize.
Seems like cookies are disabled on this browser, please enable them to open this website
Delete a Node
You are viewing a single comment's thread. Return to all comments →
Nice solutions! Here was my solution-
Later I changed it to this-
Which can delete the last element as well as can ignore delete operation which position is > ListSize.