关于前端:前端常见的手写算法问题

24次阅读

共计 412 个字符,预计需要花费 2 分钟才能阅读完成。

一 单链表反转

1 迭代

function reverseLink (link) {if (!link.head || !link.head.next) {return link} else {
      let current = link.head
      let pre = null
      while(current) {
         const next = current.next
         current.next = pre
         pre = current
         current = next
      }
      link.head = pre
   }
}

2 递归

function reverseLink (link) {
   const head = link.head
   if (!head || !head.next) {
      link.head = head
      return link
   } else {
      link.head = head.next
      reverseLink(link)
      head.next.next = head
      head.next = null
   }
}

链表中环的检测
两个有序的链表合并
删除链表倒数第 n 个结点
求链表的两头结点

正文完
 0