关注重要事件 發表於 2019-6-28 16:44:00

数据结构之-链表倒转

<p>最近在复习数据结构,整理部分有点绕的点,其中一个就是链表倒转</p>
<p><img src="https://img2018.cnblogs.com/blog/707433/201906/707433-20190628164213466-1756432841.gif"></p>
<pre><code class="language-c">/* 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-&gt;next;

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

      // Move pointers one position ahead.
      prev = current;
      current = next;
    }
    *head_ref = prev;
}
</code></pre>
<pre><code class="language-js">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))
</code></pre><br><br>
来源:https://www.cnblogs.com/johnzhu/p/11103543.html
頁: [1]
查看完整版本: 数据结构之-链表倒转