misunderstanding the question


  • 0
    M

    Given
    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8

    I thought it was 243+564 = 807 -> reverse to -> 708
    until I found 18 + 0 outputs 18 but not 81


  • 0
    M

    Actual this do not need reverse ... After I comment the reverse code

    Simple note: reverse a single list

    'use strict'
    
    class ListNode {
      constructor(val, next) {
        this.val = val
        this.next = next
      }
    
      print() {
        let ret = []
        let cur = this
        while (cur) {
          ret.push(cur.val)
          cur = cur.next
        }
        const str = ret.join(' -> ')
        console.log(str)
      }
    
      reverse() {
        let head = this
        let fi = this
    
        while (fi.next) {
          const next = fi.next
          fi.next = fi.next.next
          next.next = head
          head = next
        }
    
        return head
      }
    
      reverse2() {
        // last
        if (!this.next) {
          return this
        }
    
        // reverse left
        const result = this.next.reverse2()
    
        // append self to end
        this.next.next = this
        this.next = null
    
        return result
      }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.