查看: 3|回复: 0

数据结构之-链表倒转

[复制链接]

0

主题

0

回帖

0

积分

积极分子

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2010-3-10
发表于 2019-6-28 16:44:00 | 显示全部楼层 |阅读模式

最近在复习数据结构,整理部分有点绕的点,其中一个就是链表倒转

/* Function to reverse the linked list */
static void reverse(struct LNode **head_ref)
{
    struct LNode *prev = NULL;
    struct LNode *current = *head_ref;
    struct LNode *next = NULL;
    while (current != NULL)
    {
        // Store next
        next = current->next;

        // Reverse current node's pointer
        current->next = prev;

        // Move pointers one position ahead.
        prev = current;
        current = next;
    }
    *head_ref = prev;
}
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))


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

使用道具 举报

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

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

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

在本版发帖返回顶部