我這樣做hackerrank問題(https://www.hackerrank.com/challenges/get-the-value-of-the-node-at-a-specific-position-from-the-tail) 我的代碼如下 -獲取節點值從最終在C
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;
}
}
}
所以我所做的是,我第一次扭轉了鏈表,然後爲特定位置循環並打印其值。它是正確的方法嗎? 它給了我這個錯誤。
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
需要,因爲你的函數的原型是這麼說的,返回一個整數。我認爲這個信息很清楚。 –
是的,但(return head-> data)是一個整數。 – shreyaa
你在哪裏***'return' ***什麼?你*知道'return'語句嗎?也許你應該[讀幾本好書](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)? –