Leetcode 2141:两数相加-环形链表

Posted hello,是翠花呀

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 2141:两数相加-环形链表相关的知识,希望对你有一定的参考价值。

两数相加

  var addTwoNumbers = function (l1, l2) 

    let cur1 = l1;
    let cur2 = l2;
  
    let newLinkHead = new ListNode(0);
    let result = newLinkHead;
  
    let yu = 0; // 余数
    let carrybit = 0; // 进位
    let val1; 
    let val2;
    while (cur1 || cur2) 
      val1 = (cur1 && cur1.val) || 0;
      val2 = (cur2 && cur2.val) || 0;
  
      yu = (val1 + val2 + carrybit) % 10; 
  
      result.next = new ListNode(yu);
  
      if (val1 + val2 + carrybit >= 10)  
        carrybit = 1;
       else 
        carrybit = 0;
      
  
      result = result.next;
      cur1 = cur1 && cur1.next;
      cur2 = cur2 && cur2.next;
  
    

    if (carrybit >0)  // val1 + val2 + carrybit >= 10 
      result.next = new ListNode(carrybit);
    
  
    return newLinkHead.next; 
  ;

环形链表

var hasCycle = function (head) 
  if (!head) 
    return false
  
  let pre = head;
  let cur = head;

  while (pre && pre.next) 
    pre = pre.next.next;
    cur = cur.next;
    if (pre == cur) 
      return true
    
  
  return false

以上是关于Leetcode 2141:两数相加-环形链表的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 2141:两数相加-环形链表

Leetcode 2141:两数相加-环形链表

leetcode 链表 两数相加

LeetCode刷题笔记-数据结构-day10

LeetCode刷题笔记-数据结构-day10

Leetcode 两数相加(链表)