链表算法小结(JS)

80 阅读2分钟

链表算法汇总

移除链表元素

leetcode.cn/problems/re…

/*
 * @lc app=leetcode.cn id=203 lang=javascript
 *
 * [203] 移除链表元素
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function (head, val) {
  const dummyHead = new ListNode(0)
  dummyHead.next = head
  let pre = dummyHead
  while (head) {
    if (head.val === val) {
      pre.next = head.next
    } else {
      pre = head
    }
    head = head.next
  }
  return dummyHead.next
}
// @lc code=end

设计链表

leetcode.cn/problems/de…

/*
 * @lc app=leetcode.cn id=707 lang=javascript
 *
 * [707] 设计链表
 */

// @lc code=start
class ListNode {
  constructor(val) {
    this.val = val
    this.next = null
  }
}
var MyLinkedList = function () {
  this.dummyHead = new ListNode(0)
  this.size = 0
}

/**
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function (index) {
  if (index < 0 || index >= this.size) {
    return -1
  }
  let node = this.dummyHead
  for (let i = 0; i <= index; i++) {
    node = node.next
  }
  return node.val
}

/**
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function (val) {
  this.addAtIndex(0, val)
}

/**
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function (val) {
  this.addAtIndex(this.size, val)
}

/**
 * @param {number} index
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function (index, val) {
  if (index < 0 || index > this.size) {
    return
  }
  let pre = this.dummyHead
  for (let i = 0; i < index; i++) {
    pre = pre.next
  }
  const addNode = new ListNode(val)
  addNode.next = pre.next
  pre.next = addNode
  this.size++
}

/**
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function (index) {
  if (index < 0 || index >= this.size) {
    return
  }
  let pre = this.dummyHead
  for (let i = 0; i < index; i++) {
    pre = pre.next
  }
  pre.next = pre.next.next
  this.size--
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * var obj = new MyLinkedList()
 * var param_1 = obj.get(index)
 * obj.addAtHead(val)
 * obj.addAtTail(val)
 * obj.addAtIndex(index,val)
 * obj.deleteAtIndex(index)
 */
// @lc code=end

反转链表

leetcode.cn/problems/re…

/*
 * @lc app=leetcode.cn id=206 lang=javascript
 *
 * [206] 反转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function (head) {
  let pre = null
  while (head) {
    const cur = head
    head = head.next
    cur.next = pre
    pre = cur
  }
  return pre
}
// @lc code=end

两两交换链表中的节点

leetcode.cn/problems/sw…

/*
 * @lc app=leetcode.cn id=24 lang=javascript
 *
 * [24] 两两交换链表中的节点
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var swapPairs = function (head) {
  const dummyHead = new ListNode(0)
  dummyHead.next = head
  let pre = dummyHead
  while (pre.next && pre.next.next) {
    const cur = pre.next
    pre.next = cur.next
    cur.next = cur.next.next
    pre.next.next = cur
    pre = cur
  }
  return dummyHead.next
}
// @lc code=end

删除链表的倒数第 N 个节点

leetcode.cn/problems/re…

/*
 * @lc app=leetcode.cn id=19 lang=javascript
 *
 * [19] 删除链表的倒数第 N 个结点
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function (head, n) {
  const dummyHead = new ListNode(0)
  dummyHead.next = head
  let node = dummyHead
  for (let i = 0; i < n + 1; i++) {
    node = node.next
  }
  let pre = dummyHead
  while (node) {
    pre = pre.next
    node = node.next
  }
  pre.next = pre.next.next
  return dummyHead.next
}
// @lc code=end

相交链表

leetcode.cn/problems/in…

/*
 * @lc app=leetcode.cn id=160 lang=javascript
 *
 * [160] 相交链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function (headA, headB) {
  let curA = headA,
    curB = headB
  let lenA = 0,
    lenB = 0
  while (curA) {
    curA = curA.next
    lenA++
  }
  while (curB) {
    curB = curB.next
    lenB++
  }
  let curL = lenA > lenB ? headA : headB
  let curS = curL === headA ? headB : headA
  const steps = Math.abs(lenA - lenB)
  for (let i = 0; i < steps; i++) {
    curL = curL.next
  }
  while (curL) {
    if (curL === curS) {
      return curL
    }
    curL = curL.next
    curS = curS.next
  }
  return null
}
// @lc code=end

环形链表

leetcode.cn/problems/li…

/*
 * @lc app=leetcode.cn id=141 lang=javascript
 *
 * [141] 环形链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function (head) {
  let fast = head,
    slow = head
  while (fast && fast.next) {
    fast = fast.next.next
    slow = slow.next
    if (fast === slow) break
  }
  if (!fast || !fast.next) return false
  return true
}
// @lc code=end

环形链表 II

leetcode.cn/problems/li…

/*
 * @lc app=leetcode.cn id=142 lang=javascript
 *
 * [142] 环形链表 II
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var detectCycle = function (head) {
  let fast = head,
    slow = head
  while (fast && fast.next) {
    fast = fast.next.next
    slow = slow.next
    if (fast === slow) break
  }
  if (!fast || !fast.next) return null
  slow = head
  while (fast !== slow) {
    fast = fast.next
    slow = slow.next
  }
  return fast
}
// @lc code=end