0

Book picture 1 Book picture

I started with basic linkedlist operations.

This is a program to insert a new node at the beginning or a specific position in linkedlist.

I understand the first picture has the case where if linked list is empty insert return new node.

  1. If invalid position for insertion, return head.
  2. Last if else block, if position is at the beginning insert new node at the beginning, point next to head and return new node.
  3. I understand what else block is doing, inserting at a specific position.

Question (I know it is dumb): why are we returning headNode at the end if we had returned a node in every case/block mentioned above? Should it not be returned inside the last else block but not outside?

Gik
  • 9
  • 2

2 Answers2

1

We are returning the head node in all the 4 cases. Head node is always the first node in the linked list.

In case 1 : - When Linked List is empty, create a new node and return it. Since there is only 1 node the returned node is the head node.

In case 2 : - If insertion position is invalid we return the head node.

In case 3 :- When insertion position is at the beginning, we set the next of the new node to the current head and return the new node which now is our new head node, since it is at the beginning.

In case 4 :- When we insert at specific position, post insertion and changing links we return the head node again.

To understand why we always return the head node please read below-

Method signature -

ListNode insert(ListNode NewNode , int position)

Now the responsibility of this method is to just insert the node at the specific position and always give the head node to the caller of this method. So that the caller can iterate through the linked list from the beginning to the end.

Suppose there is a Linked list of 10 nodes and if you insert the new node at the end of the linked list, and you return the new node instead of the head node. Now the caller can not go back in the linked list as nodes always point to the next node, so caller will think that the link list only contains 1 node.

Shubham Chopra
  • 1,678
  • 17
  • 30
  • My specific question is why are we returning it outside the else block ? And not inside. Because if we have a common headNode returned for the whole program then in case 3 headNode is not HEAD, new node is HEAD, so If common headNode is returned for the program and insertion happens at the beginning then headNode still points to the second element and not the first. I am not sure if this statement of mine makes sense – Gik Nov 19 '20 at 05:51
  • Got it, in all other cases except 4, code will return early before it reaches the last return statement. Your statement - "so If common headNode is returned for the program and insertion happens at the beginning then headNode still points to the second element and not the first." - This will not happen as the code flow will not reach the last statement we are returning early inside the if block, if we insert at the beginning. Once we return from a function the code below it does not execute. – Shubham Chopra Nov 19 '20 at 06:48
0

Whether to return the headNode before or after the closing brace of the else-block is just a matter of coding style. It does not change the result.

I agree that returning from the else-block would be more consistent.

In java, there is no reason not to move the return statement into the else-block. If you forget to return from one of the branches, the compiler will yield an error, no problem. The style shown in the book is perhaps carried over from a language where "falling off the edge of a non-void function" can silently lead to serious problems, like (old versions of) C.

Another possible style that is aimed at "removing clutter" is to omit the elses entirely if every if branch returns. See for example this question, although it is about JS, not Java.

Hulk
  • 6,399
  • 1
  • 30
  • 52
  • @Gik if this answered your question, you might consider accepting this answer by clicking the checkmark button bext to it. – Hulk Nov 19 '20 at 06:27