一 单链表反转

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 个结点
求链表的中间结点


小葱
95 声望3 粉丝