leetcode 算法题:删除排序链表中的重复元素、环形链表、二叉树的层序遍历

691 阅读2分钟

83. 删除排序链表中的重复元素

image.png

解题思路
  • 因为链表是有序的,所以重复元素是相邻的
  • 遍历链表,如果发现当前元素和下个元素值相同就删除下个元素值
  • 遍历链表,如果发现当前元素和下个元素值相同,就删除下个元素的值
  • 遍历结束,返回原链表的头部
/**
 * 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 deleteDuplicates = function(head) {
    let p1 = head
    while(p1 && p1.next) {

        if(p1.val === p1.next.val) {
            p1.next = p1.next.next
        }else{
            p1 = p1.next;
        }
    }
    return head;
};

141. 环形链表

image.png

解题思路
  • 两个人在圆形操场上的起点同时起跑,速度快的人一定会超过速度慢的人一圈
  • 用一快一慢的两个指针遍历链表,如果指针能够相逢,那么链表就有环
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function(head) {
    // 排除空链表情况
    if(!head) { return false};
    let p1 = head;
    let p2 = head;
    // 使用p2 进行非空判断
    while(p2.next !== null && p2.next.next !== null) {
        p1 = p1.next;
        p2 = p2.next.next;
        if(p1 === p2) {
            return true;
        }
    }
    return false;
};

102. 二叉树的层序遍历

image.png

解题思路
  • 层序遍历就是广度优先遍历
  • 在遍历时候需要记录当前节点所处的层级,方便将其添加到不同的数组中
  • 广度优先遍历二叉树
  • 遍历过程中,记录每个节点的层级,并将其添加到不同的数组中
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrder = function(root) {
        // 存放层级数据
        let res = [];
        // 空直接返回
        if(!root){ return []}
        // 把头部加入队列 [值,层级]
        const queue = [[root,0]];
        while(queue?.length) {
            const [c,l] = queue.shift();
            // 放入对应层级的二维数据
            res[l] ? res[l].push(c.val)  :  res[l] = [c.val] ;
            c?.left &&  queue.push([c.left,l + 1]);
            c?.right && queue.push([c.right, l +1]); 
        }
    return res;
};

94. 二叉树的中序遍历

image.png

解题思路
  • 中序遍历(递归版和非递归版)
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function(root) {
    /**
    // 递归版
    const res =[];
    const rec = (n) => {
        if(!n) { return null}
        n.left && rec(n.left);
        res.push(n.val)
        n.right && rec(n.right)
    }
    rec(root)
    return res;
    **/
    const res =[];
    const stack = [];
    let p = root;
    while(stack.length || p) {
     while(p) {
        stack.push(p)
        p = p.left;
     }
     //  访问左节点
     const n = stack.pop();
     res.push(n.val)
     // 把指针指向右节点
     p = n.right
    }
    return res;
};