数据结构进阶篇,链表专题

2. 两数相加

  「题目:」

  给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

  请你将两个数相加,并以相同形式返回一个表示和的链表。

  你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

  「示例:」

  输入:l1 = [2,4,3], l2 = [5,6,4], 输出:[7,0,8], 解释:342 + 465 = 807.

  「解题思路:」

  模拟两个链表相加的过程,需要注意以下几点:

  • 两个结点之和 = l1.val + l2.val + carry(进位值)。

  • 和的链表对应的结点值 = (l1.val + l2.val + carry) % 10。

  • 由于两个链表长度不一,所以需要对取值和迭代操作做健壮性处理。

  • 链表遍历结束之后,一定要再判断 carry 是否还有值,这一点特别容易遗漏。

  时间复杂度:O(max(n, m)),空间复杂度:O(1)。

const addTwoNumbers = function(l1, l2) {
    const ans = new ListNode(null);

    let currentHead = ans;
    let carry = 0;
    while (l1 || l2) {
        const x = (l1 && l1.val) || 0;
        const y = (l2 && l2.val) || 0;
        let sum = x + y + carry;
        if (sum >= 10) {
            carry = 1;
        } else {
            carry = 0;
        }

        currentHead.next = new ListNode(sum % 10);
        currentHead = currentHead.next;
        
        l1 && (l1 = l1.next);
        l2 && (l2 = l2.next);
    }

    if (carry !== 0) {
        currentHead.next = new ListNode(carry);
    }

    return ans.next;
};

445. 两数相加 II

  「题目:」

  给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。

  你可以假设除了数字 0 之外,这两个数字都不会以零开头。

  「示例:」

  输入:l1 = [7,2,4,3], l2 = [5,6,4], 输出:[7,8,0,7].

  「第一种解题思路:」

  相比较上一题,区别在于数字的高位位于链表开始位置,所以没办法直接通过遍历链表来模拟两数相加。

  朴素的方式就是先将两个链表进行翻转,然后模拟两数相加,最后再将和值链表再次翻转。

  时间复杂度:O(max(m, n)),空间复杂度:O(1)。

const addTwoNumbers = (l1, l2) => {
  const ans = new ListNode(null);

  let currentHead = ans;
  let carry = 0;

  l1 = reverseLinkedList(l1);
  l2 = reverseLinkedList(l2);

  while (l1 || l2) {
    const x = (l1 && l1.val) || 0;
    const y = (l2 && l2.val) || 0;

    const sum = x + y + carry;
    if (sum >= 10) {
      carry = 1;
    } else {
      carry = 0;
    }

    currentHead.next = new ListNode(sum % 10);
    currentHead = currentHead.next;

    l1 && (l1 = l1.next);
    l2 && (l2 = l2.next);
  }

  if (carry > 0) {
    currentHead.next = new ListNode(carry);
  }

  return reverseLinkedList(ans.next);

}

function reverseLinkedList(head) {
  if (!head || !head.next) {
    return head;
  }

  let nextHead = head.next;
  head.next = null;

  while (nextHead) {
    const temp = nextHead.next;
    nextHead.next = head;
    head = nextHead;
    nextHead = temp;
  }

  return head;
}

24. 两两交换链表中的节点

  「题目:」

  给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

  「示例:」

  输入:head = [1,2,3,4], 输出:[2,1,4,3].

  「递归解题思路:」

  递归的终止条件是链表中没有节点或者只有一个节点,此时无法进行节点交换。

  在链表进行两两交换的过程中,原始链表的头节点变成新链表的第二个节点,原始链表的第二个节点变成新链表的头节点,按照这样的方式,后续节点可以递归地实现。

  时间复杂度:O(n),空间复杂度:O(n)。

const swapPairs = head => {
    if (head === null || head.next === null) {
        return head;
    }
    const newHead = head.next;
    head.next = swapPairs(newHead.next);
    newHead.next = head;
    return newHead;
}

  「迭代解题思路:」

  首先需要创建 dummyHead 来处理头节点交换的场景,然后获取到旧链表的头节点和第二个节点两两交换即可。

  时间复杂度:O(n),空间复杂度:O(1)。

const swapPairs = head => {
    const dummyHead = new ListNode(null);
    dummyHead.next = head;

    let temp = dummyHead;

    while (temp.next !== null && temp.next.next !== null) {
        const node1 = temp.next;
        const node2 = temp.next.next;

        temp.next = node2;
        node1.next = node2.next;
        node2.next = node1;
        temp = node1;
    }

    return dummyHead.next;
}

写在最后

  「感谢您能耐心地读到这里,如果本文对您有帮助,欢迎点赞、分享、或者关注下方的公众号哟。」

  相关链接:

  • https://leetcode-cn.com/problems/add-two-numbers/

  • https://leetcode-cn.com/problems/add-two-numbers-ii/

  • https://leetcode-cn.com/problems/swap-nodes-in-pairs/

b6e7cf7cd1379fa34f7b9bcd1c0a5820.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值