查看: 100|回复: 0

数据结构之-反向打印链表

[复制链接]

5

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2008-5-2
发表于 2021-5-11 14:31:00 | 显示全部楼层 |阅读模式
  1. 通过数组或者栈保存元素来实现

  2. 翻转链表,再打印

reversePrint  = function(nodes){
 let prev= null;
 let next= null;
//  let cur = nodes
  while(nodes){
    next = nodes.next
    
    nodes.next = prev
    
    prev = nodes;
    
    nodes = next
  }
 return prev
}

var nodes = {
 val:1,
 next:{
   val:2,
   next:{
     val:3,
     next:null
   }
 }
}

var res = reversePrint(nodes)

console.log('res',JSON.stringify(res))
  1. 递归
reversePrint  = function(nodes){
 if(!nodes) return
 if(nodes.next !=null) reversePrint(nodes.next)
 console.log(nodes.val)
 
}

var nodes = {
 val:1,
 next:{
   val:2,
   next:{
     val:3,
     next:null
   }
 }
}

var res = reversePrint(nodes)


来源:https://www.cnblogs.com/johnzhu/p/14755182.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

圆梦公社,专注于为全球华人提供纯粹技术交流的地方,请勿发布任何政治及违法的言论。如有相关侵权、举报、投诉及建议等,请发 E-mail:dzh188@hotmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部