How do you find length of a Singly Linked list
2013-11-15 16:37
302 查看
Here is one of the classical questions asked to me on an
interview with multinational Investmentbank after that this question asked to me on several times in other interviews also . what makes this question interesting is that java
developers are not that great with data structure relative to C++ developer which is obvious because of fundamental difference between this two language.C++ is more of system programming language while java is more on application programming , also rich set
of java API allows programmer to skip this kind of basic programming techniques.
anyway lets come back to the question , everybody knows that Linked lists is last element will point to "null" element , so first
answer would most of the times would be "I will use a counter and increment till we reach the end of
the element" e.g.
Iterative Solutions
public int length(){
int count=0;
Node current = this.head;
while(current != null){
count++;
current=current.next()
}
return count;
}
If you answer this question without any difficulty most interviewer will ask you to write a "recursive"
solution for this just
to check how you deal with recursion if your first answer would have been recursive they will ask you to write an "iterative solution" as shown above.
Recursive Solution:
public int length(Node current){
if(current == null) //base case
return 0;
return 1+length(current.next());
}
Read more: http://javarevisited.blogspot.com/2010/10/how-do-you-find-length-of-singly-linked.html#ixzz2khZ1aahc
interview with multinational Investmentbank after that this question asked to me on several times in other interviews also . what makes this question interesting is that java
developers are not that great with data structure relative to C++ developer which is obvious because of fundamental difference between this two language.C++ is more of system programming language while java is more on application programming , also rich set
of java API allows programmer to skip this kind of basic programming techniques.
anyway lets come back to the question , everybody knows that Linked lists is last element will point to "null" element , so first
answer would most of the times would be "I will use a counter and increment till we reach the end of
the element" e.g.
Iterative Solutions
public int length(){
int count=0;
Node current = this.head;
while(current != null){
count++;
current=current.next()
}
return count;
}
If you answer this question without any difficulty most interviewer will ask you to write a "recursive"
solution for this just
to check how you deal with recursion if your first answer would have been recursive they will ask you to write an "iterative solution" as shown above.
Recursive Solution:
public int length(Node current){
if(current == null) //base case
return 0;
return 1+length(current.next());
}
Read more: http://javarevisited.blogspot.com/2010/10/how-do-you-find-length-of-singly-linked.html#ixzz2khZ1aahc
相关文章推荐
- 与字符串数组有关的三个函数
- Like 作用于汉字查找的表示方法
- VBA 处理字符串
- 字体实时变化代码
- 大战电脑病毒
- VBA计算某个集合中哪6个数字的和为…
- 浅谈DICTIONARY(字典)对象
- ADO连接数据库字符串大全
- Excel lookup应用-从字符串取数
- linux定时任务的设置
- VBA窗体模拟显示进度条
- 实现动态图表渐变效果的代码…
- Excel制作三级动态下拉菜单
- Shell Trap tips
- 单例模式
- 转 Using $.ajaxPrefilter() To Configure AJAX Requests In jQuery 1.5
- EXCEL使用宏破解工作表保护密码
- Excel 利用日期控件输入时间
- 解决每次打开Word、Excel等都要重…
- Excel 公式提取文件名