数据结构之-链表倒转

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

/* 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))
posted @ 2019-06-28 16:44  小猪ab  阅读(190)  评论(0编辑  收藏  举报