DrS*_*ove 3 javascript linked-list
var someList = {
data : 1,
next : {
data : 2,
next : {
data : 3,
next : {
data : 4,
next : null
}
}
}
};
var ar = [];
function reversePrint( LList )
{
var c = null;
c = LList;
while ( c.next != null )
{
ar.unshift( c.data );
c = c.next;
}
console.log( ar );
}
Run Code Online (Sandbox Code Playgroud)
该例程以相反的顺序输出数组中的数据.
问题是:循环没有得到data : 4.
如何重写它以输出所有数据?
for (var c = LList; c; c = c.next) {
// do something with c.data
}
Run Code Online (Sandbox Code Playgroud)