由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++ Q76: singly linked list -- 这个逆序打印有什么错?
相关主题
[合集] C++ Q76: singly linked list -- 这个逆序打印有什么错?请教amazon面试题
明天电面,求建议leetcode上这个链表节点的定义是什么意思?ListNode(int x) : val(x), next(NULL) {}
M家 onsite 悲剧,同胞们弄死烙印吧怎么理解递归解决的“swap every two elements in a linked list”?
remove a node in O(1) from link listTripAdvsior 面经 (完败)
remove a node (and its memory) from a doubly linked list问一个面试题
leetcode 一道简单题的疑问实现next_permutation
Careercup书第四版一道题的解答有错问下嵌入式/DSP软件开发面试也需要刷题么?
LeetCode:Partition List 哪位帮我看看, 为什么总是TLE刚面完的2道题,我做的稀烂
相关话题的讨论汇总
话题: head话题: cout话题: endl话题: null
进入JobHunting版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
void reversePrint(Node* head) {
if(head=NULL) {
cout << "Empty List. No node to print." << endl;
return;
}
if(head->next=NULL) {
cout << head->data << endl;
return;
} else {
Node* temp=head;
head=temp->next;
reversePrint(head);
cout << temp->data << endl;
return;
}
}
s*****y
发帖数: 897
2
.....
did not look into detail, but the first look find that
head = NULL...

【在 c**********e 的大作中提到】
: void reversePrint(Node* head) {
: if(head=NULL) {
: cout << "Empty List. No node to print." << endl;
: return;
: }
: if(head->next=NULL) {
: cout << head->data << endl;
: return;
: } else {
: Node* temp=head;

c**********e
发帖数: 2007
3
You are right. Thanks. The old == versus = problem.

【在 s*****y 的大作中提到】
: .....
: did not look into detail, but the first look find that
: head = NULL...

c**********e
发帖数: 2007
4
发现不定义新节点也没问题。
void reversePrint(Node* head) {
if(head==NULL) {
cout << "Empty List. No node to print." << endl;
return;
}
if(head->next==NULL) {
cout << head->data << endl;
return;
} else {
reversePrint(head->next);
cout << head->data << endl;
return;
}
}
c**y
发帖数: 2282
5
你这Q+数字玩的是什么啊

【在 c**********e 的大作中提到】
: 发现不定义新节点也没问题。
: void reversePrint(Node* head) {
: if(head==NULL) {
: cout << "Empty List. No node to print." << endl;
: return;
: }
: if(head->next==NULL) {
: cout << head->data << endl;
: return;
: } else {

c**********e
发帖数: 2007
6
开始是 Brainbench 的练习题。后来加的就是我自己想问的题目了,或者是面试题,或
者是我自己遇到的问题。

【在 c**y 的大作中提到】
: 你这Q+数字玩的是什么啊
R****i
发帖数: 104
7
Only the last one will be printed.

【在 c**********e 的大作中提到】
: void reversePrint(Node* head) {
: if(head=NULL) {
: cout << "Empty List. No node to print." << endl;
: return;
: }
: if(head->next=NULL) {
: cout << head->data << endl;
: return;
: } else {
: Node* temp=head;

c**********e
发帖数: 2007
8
Did you try it? I tried it. It printed all.

【在 R****i 的大作中提到】
: Only the last one will be printed.
t******g
发帖数: 252
9
"Empty List. No node to print"
That will always be printed out first.
t******g
发帖数: 252
10
Actually no. This won't happen. But it's better to have this out of the
recursion.

【在 t******g 的大作中提到】
: "Empty List. No node to print"
: That will always be printed out first.

相关主题
leetcode 一道简单题的疑问请教amazon面试题
Careercup书第四版一道题的解答有错leetcode上这个链表节点的定义是什么意思?ListNode(int x) : val(x), next(NULL) {}
LeetCode:Partition List 哪位帮我看看, 为什么总是TLE怎么理解递归解决的“swap every two elements in a linked list”?
进入JobHunting版参与讨论
d**e
发帖数: 6098
11
你发现有什么错?感觉没什么错误.
但如果说以整个code来看,其实后面两个return是多余的,可删去,另外print的message
有bug,head是空就说是empty list,但因为用了递归,head->next为null时,不代表它是
empty list
手痒写了个,但没验证...
void reversePrint(Node * head){
if(!head){
cout << "the end ..." << endl; // print some message here if needed...
return;
}
reversePrint(head->next);
cout << head->data << endl;
}

【在 c**********e 的大作中提到】
: void reversePrint(Node* head) {
: if(head=NULL) {
: cout << "Empty List. No node to print." << endl;
: return;
: }
: if(head->next=NULL) {
: cout << head->data << endl;
: return;
: } else {
: Node* temp=head;

c**********e
发帖数: 2007
12
use head==NULL, not head=NULL (this is always true).

【在 t******g 的大作中提到】
: "Empty List. No node to print"
: That will always be printed out first.

c**********e
发帖数: 2007
13

你是对的。后面那两个return语句是多余的,因为程序会自然运行。
这个message应该没错。因为只有这个list为空时,这个message才会打印。否则第二个
return处理最终状态。
验证了一下,你是对的。并且你的处理终止状态比我的简单。
...

【在 d**e 的大作中提到】
: 你发现有什么错?感觉没什么错误.
: 但如果说以整个code来看,其实后面两个return是多余的,可删去,另外print的message
: 有bug,head是空就说是empty list,但因为用了递归,head->next为null时,不代表它是
: empty list
: 手痒写了个,但没验证...
: void reversePrint(Node * head){
: if(!head){
: cout << "the end ..." << endl; // print some message here if needed...
: return;
: }

c**********e
发帖数: 2007
14
第二个return语句保证了message不会被打印,除非原来的list为空。

【在 t******g 的大作中提到】
: Actually no. This won't happen. But it's better to have this out of the
: recursion.

d**e
发帖数: 6098
15
噢....谢谢,那你原来上面的return是有用的,我当时想的时候是把两个return去掉后才
觉得那个message有错.

【在 c**********e 的大作中提到】
: 第二个return语句保证了message不会被打印,除非原来的list为空。
c**********e
发帖数: 2007
16
完全不用return也是可以的。你的可以改成
void reversePrint(Node * head){
if(!head){
cout << "the end ..." << endl; // print some message here if needed...
} else {
reversePrint(head->next);
cout << head->data << endl;
}
}
我的可以改成
void reversePrint2(Node* head) {
if(head==NULL) {
cout << "Empty List. No node to print." << endl;
} else if(head->next==NULL) {
cout << head->data << " reversePrint2" << endl;
} else {
reversePrint2(head->next);
cout << head->data << " reversePrint2" << endl;
}
}

message
...

【在 d**e 的大作中提到】
: 你发现有什么错?感觉没什么错误.
: 但如果说以整个code来看,其实后面两个return是多余的,可删去,另外print的message
: 有bug,head是空就说是empty list,但因为用了递归,head->next为null时,不代表它是
: empty list
: 手痒写了个,但没验证...
: void reversePrint(Node * head){
: if(!head){
: cout << "the end ..." << endl; // print some message here if needed...
: return;
: }

s*****y
发帖数: 897
17
Done's code is better.
void reversePrint(Node * head){
if(!head){
cout << "the end ..." << endl; // print some message here if needed..
.
} else {
reversePrint(head->next);
cout << head->data << endl;
}
}
Suppose you write this function and this function is very very long. Also, m
aybe there are a lot of #ifdef, #endif inside this function to support multi
ple platform. People look at this kind of code often get loss.
Then later on, another engineer jump in and modify this function by adding c
ode behind here:
} else {
reversePrint(head->next);
cout << head->data << endl;
}
/*****************/
add code here
/*****************/
}
But he did not pay attention to the code you write previously as the functio
n is so big. Normally, it should just return when the head is NULL. But now
it continue excuting without return.....Bug happens then.

...

【在 c**********e 的大作中提到】
: 完全不用return也是可以的。你的可以改成
: void reversePrint(Node * head){
: if(!head){
: cout << "the end ..." << endl; // print some message here if needed...
: } else {
: reversePrint(head->next);
: cout << head->data << endl;
: }
: }
: 我的可以改成

c**********e
发帖数: 2007
18
I agree. It is better to use return than to use if else.

..

【在 s*****y 的大作中提到】
: Done's code is better.
: void reversePrint(Node * head){
: if(!head){
: cout << "the end ..." << endl; // print some message here if needed..
: .
: } else {
: reversePrint(head->next);
: cout << head->data << endl;
: }
: }

l******u
发帖数: 22
19
It should work, but just is not very simple. We may be try following more
simple codes:
void reversePrint(Node *head){
if(head == NULL) return;
reversePrint(head->next);
cout << head->data << "\t";
}
c**********e
发帖数: 2007
20
It is the same as done's.

【在 l******u 的大作中提到】
: It should work, but just is not very simple. We may be try following more
: simple codes:
: void reversePrint(Node *head){
: if(head == NULL) return;
: reversePrint(head->next);
: cout << head->data << "\t";
: }

1 (共1页)
进入JobHunting版参与讨论
相关主题
刚面完的2道题,我做的稀烂remove a node (and its memory) from a doubly linked list
刚才看到小尾羊的一个面试题leetcode 一道简单题的疑问
一个Linkedlist面试题的教训Careercup书第四版一道题的解答有错
一道链表题及其变种LeetCode:Partition List 哪位帮我看看, 为什么总是TLE
[合集] C++ Q76: singly linked list -- 这个逆序打印有什么错?请教amazon面试题
明天电面,求建议leetcode上这个链表节点的定义是什么意思?ListNode(int x) : val(x), next(NULL) {}
M家 onsite 悲剧,同胞们弄死烙印吧怎么理解递归解决的“swap every two elements in a linked list”?
remove a node in O(1) from link listTripAdvsior 面经 (完败)
相关话题的讨论汇总
话题: head话题: cout话题: endl话题: null