0

I am doing this hackerrank question (https://www.hackerrank.com/challenges/get-the-value-of-the-node-at-a-specific-position-from-the-tail) My code is as follows -

int GetNode(Node *head,int positionFromTail)
{
  Node *prev = NULL;
  Node *current = head;
  Node *next;
  while(current!=NULL){
     next = current->next;
     current->next = prev;
     prev = current;
     current = next;
  }
  head = prev;
  int p=0;
  while(head->next!=NULL){
    if(p== positionFromTail){
        return head->data;
    }
    else {
        p++;
        head= head->next;
    }
  } 
}

So what i have done is, I have first reversed the linkedlist and then looped for the specific position and then printed its value. Is it correct method to do it? It gives me this error.

  solution.cc: In function ‘int GetNode(Node*, int)’:
  solution.cc:42:1: error: control reaches end of non-void function [Werror=return-type]
   }
   ^
   cc1plus: some warnings being treated as errors
alk
  • 69,737
  • 10
  • 105
  • 255
shreyaa
  • 93
  • 3
  • 14
  • 1
    you need to return an integer because the prototype of your function says so. I thought the message was clear. – Jean-François Fabre Aug 31 '17 at 15:48
  • yes , but (return head->data) is an integer. – shreyaa Aug 31 '17 at 15:52
  • Where do you ***`return`*** anything? You *do* know about the `return` statement? Perhaps you should [read a couple of good books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)? – Some programmer dude Aug 31 '17 at 15:53
  • Your function needs to return an integer in every case of your if conditions, or simply you can return an integer at the end of the function. Problem here is the compiler thinks that nothing is being returned when the condition if(p== positionFromTail) never becomes true. – Vishwas Aug 31 '17 at 15:55

2 Answers2

1

The problem statement makes it impossible for the code to reach the end of your function without returning a value, because of this constraint:

Constraints

Position will be a valid element in linked list.

However, C compiler has no idea that your while loop would never exit upon reaching NULL, guaranteeing that return head->data is eventually executed, so it issues an error.

You can fix this by providing an unused return at the end, or by making your loop infinite.

Note: Your solution reverses the list, which may be non-optimal. You can avoid reversal by storing positionFromTail + 1 trailing items in an array as you traverse the list once:

int GetNode(Node *head,int positionFromTail) {
    int data[++positionFromTail], p = 0;
    while (head) {
        data[p] = head->data;
        head = head->next;
        p = (p+1) % positionFromTail;
    }
    return data[p];
}
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Each possible branch which might leave the function needs to return a value.

If the initial head->next would be NULL the return statement you coded would not be reached.

Design your code to have a function have only one possible exit-point.

This might look like the following:

/* returns pay-load or INT_MIN if list is empty or searched pos is negativ*/

int GetNode(Node *head, int positionFromTail)
{
  int result = INT_MIN;

  ...

  while (head->next != NULL) {
    if(p == positionFromTail) {
      result = head->data;
      break;
    }
    else {
      p++;
      head = head->next;
    }
  } 

  return result;
}
alk
  • 69,737
  • 10
  • 105
  • 255