链表
Posted BillGates--
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了链表相关的知识,希望对你有一定的参考价值。
206. 反转链表
1 迭代法反转:
var reverseList = function(head){ let prev = null; let curr = head; while(curr){ const next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; }
2 递归法反转:
var reverseList = function(head){ if(head==null || head.next==null){ return head; } // 遍历到最后一个节点 const newHead = reverseList(head.next); head.next.next = head; head.next=null; return newHead; }
以上是关于链表的主要内容,如果未能解决你的问题,请参考以下文章