首页 > 试题广场 >

链表相加(二)

[编程题]链表相加(二)
  • 热度指数:209493 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
假设链表中每一个节点的值都在 0 - 9 之间,那么链表整体就可以代表一个整数。
给定两个这种链表,请生成代表两个整数相加值的结果链表。
数据范围:,链表任意值
要求:空间复杂度 ,时间复杂度

例如:链表 1 为 9->3->7,链表 2 为 6->3,最后生成新的结果链表为 1->0->0->0。

示例1

输入

[9,3,7],[6,3]

输出

{1,0,0,0}

说明

如题面解释     
示例2

输入

[0],[6,3]

输出

{6,3}

备注:


说明:本题目包含复杂数据结构ListNode,点此查看相关信息
package main
import . "nc_tools"

// import "fmt"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
 * 
 * @param head1 ListNode类 
 * @param head2 ListNode类 
 * @return ListNode类
*/
func addInList( head1 *ListNode ,  head2 *ListNode ) *ListNode {
    p1,p2:=reverse(head1),reverse(head2)
    var ans,tail *ListNode
    carry:=0
    for p1!=nil||p2!=nil{
        val:=carry
        if p1!=nil{
            val+=p1.Val
            p1=p1.Next
        }
        if p2!=nil{
            val+=p2.Val
            p2=p2.Next
        }
        carry=val/10
        val%=10
        node:=&ListNode{Val: val}
        if ans==nil{
            ans=node
            tail=ans
        }else{
            tail.Next=node
            tail=tail.Next
        }
    }
    if carry!=0{
        tail.Next=&ListNode{Val: carry}
    }
    return reverse(ans)
}

func reverse(head *ListNode)*ListNode{
    p,q:=head,head.Next
    for q!=nil{
        tmp:=q.Next
        q.Next=p
        p=q
        q=tmp
    }
    head.Next=nil
    return p
}

发表于 2023-02-12 18:58:39 回复(0)
模拟即可,先将两个链表反转,然后以head1为基础相加
package main
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
 * 
 * @param head1 ListNode类 
 * @param head2 ListNode类 
 * @return ListNode类
*/
func addInList( head1 *ListNode ,  head2 *ListNode ) *ListNode {
    rhead1 := reverseList(head1)
    rhead2 := reverseList(head2)
    result := rhead1
    var preHead *ListNode
    plus := 0
    for rhead1 != nil || rhead2 != nil {
        if rhead1 != nil && rhead2 != nil {
            rhead1.Val, plus = add(rhead1.Val, rhead2.Val, plus)
            preHead = rhead1
            rhead1 = rhead1.Next
            rhead2 = rhead2.Next
        } else if rhead1 != nil {
            rhead1.Val, plus = add(rhead1.Val, 0, plus)
            preHead = rhead1
            rhead1 = rhead1.Next
        } else if rhead2 != nil {
            rhead2.Val, plus = add(rhead2.Val, 0, plus)
            preHead.Next = rhead2
            rhead2 = rhead2.Next
            preHead = preHead.Next
        }
    }
    if plus > 0 {
        preHead.Next = &ListNode{Val: 1, Next: nil}
        preHead = preHead.Next
    }
    return reverseList(result)
}

func add(a, b, plus int) (int,int) {
    result := a + b + plus
    if result > 9 {
        plus = 1
    } else {
        plus = 0
    }
    result = result % 10
    return result, plus
}

func reverseList(head *ListNode) *ListNode {
    root := &ListNode{Val: -1, Next: nil}
    for head != nil {
        cache := head.Next
        head.Next = root.Next
        root.Next = head
        head = cache
    }
    return root.Next
}


发表于 2022-08-08 14:33:58 回复(0)
func addInList( head1 *ListNode ,  head2 *ListNode ) *ListNode {
    // write code here
    head1=reverse(head1)
    head2=reverse(head2)
    var h *ListNode
    c:=0
    v1:=0
    v2:=0
    v:=0
    for head1!=nil ||head2!=nil{
        if head1==nil{
            v1=0
        }else{
            v1=head1.Val
            head1=head1.Next
        }
        if head2==nil{
            v2=0
        }else{
            v2=head2.Val
            head2=head2.Next
        }
        v=v1+v2+c
        if v>=10{
            c=1
            v=v-10
        }else{
            c=0
        }
        h=&ListNode{
            Val:v,
            Next:h,
        }
    }
    if c>0{
        h=&ListNode{
            Val:1,
            Next:h,
        }
    }
    return h
}
func reverse(head *ListNode) *ListNode{
    var pre *ListNode
    for head!=nil{
        t:=head.Next
        head.Next=pre
        pre=head
        head=t
    }
    return pre
}
发表于 2022-03-02 14:38:04 回复(0)
func addInList( head1 *ListNode ,  head2 *ListNode ) *ListNode {
    // write code here
    head1 = recover(head1)
    head2 = recover(head2)
    var res *ListNode
    var more int
    for head1 != nil || head2 != nil || more > 0 {
        var sum int
        if head1 != nil {
            sum += head1.Val
            head1 = head1.Next
        }
        if head2 != nil {
            sum += head2.Val
            head2 = head2.Next
        }
        res = &ListNode{
            Val: (sum+more)%10,
            Next: res,
        }
        more = (sum+more)/10
    }
    return res
}

func  recover(head *ListNode) *ListNode {
    var pre *ListNode
    var next *ListNode
    for head != nil {
        next = head.Next
        head.Next = pre
        pre = head
        head = next
    }
    return pre
}

发表于 2021-12-20 22:49:56 回复(0)
func addInList(head1 *ListNode, head2 *ListNode) *ListNode {
	head1 = reversal(head1)
	head2 = reversal(head2)

	result := new(ListNode)
	tmp := result

	// 这题和 string 加法一样的操作
	add := 0 // 进位
	cur := 0 // 当前位的值
	for head1 != nil || head2 != nil || add != 0 {
		if head1 == nil && head2 != nil {
			cur = head2.Val + add
		} else if head1 != nil && head2 == nil {
			cur = head1.Val + add
		} else if head1 == nil && head2 == nil {
			cur = add
		} else {
			cur = head2.Val + head1.Val + add
		}

		if cur > 9 {
			add = cur / 10
			cur -= 10
		} else {
			add = 0
		}

		tmp.Next = &ListNode{Val: cur, Next: nil}
		tmp = tmp.Next

		if head1 != nil {
			head1 = head1.Next
		}

		if head2 != nil {
			head2 = head2.Next
		}
	}

  // 最后别忘了反转回来
	return reversal(result.Next)
}

// 反转链表
func reversal(head *ListNode) *ListNode {
	pre := head
	var cur *ListNode = nil

	for pre != nil {
		tmp := pre.Next
		pre.Next = cur
		cur = pre
		pre = tmp
	}

	return cur
}
时间复杂度 O(n) 空间复杂度 O(1)

这题和 [NC1 大数加法](https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475) 的做法基本一致,只不过需要反转链表

发表于 2021-11-17 13:17:38 回复(0)
package main
import . "nc_tools"
/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
 * 
 * @param head1 ListNode类 
 * @param head2 ListNode类 
 * @return ListNode类
*/
func addInList( head1 *ListNode ,  head2 *ListNode ) *ListNode {
    // write code here
    stack1 := make([]int, 0)
    stack2 := make([]int, 0)
    
    for head1 != nil {
        stack1 = append(stack1, head1.Val)
        head1 = head1.Next
    }
    
    for head2 != nil {
        stack2 = append(stack2, head2.Val)
        head2 = head2.Next
    }
    
    var tmp = 0
    var head *ListNode
    for len(stack1) != 0 || len(stack2) != 0 || tmp != 0{
        var x, y = 0, 0
        if len(stack1) != 0 {
            x = stack1[len(stack1)-1]
            stack1 = stack1[:len(stack1)-1]
        }
        if len(stack2) != 0 {
            y = stack2[len(stack2)-1]
            stack2 = stack2[:len(stack2)-1]
        }
        sum := x + y + tmp
        tmp = sum/10
        
        newNode := &ListNode{
            Val: sum%10,
            Next : head,
        }
        head = newNode
    }
    return head
}

发表于 2021-11-01 16:52:20 回复(0)
func addInList(head1 *ListNode, head2 *ListNode) *ListNode {
    // write code here
    stack1 := make([]int, 0)
    stack2 := make([]int, 0)
    for head1 != nil {
        stack1 = append(stack1, head1.Val)
        head1 = head1.Next
    }
    for head2 != nil {
        stack2 = append(stack2, head2.Val)
        head2 = head2.Next
    }

    carry := 0
    var head *ListNode
    for len(stack1) > 0 || len(stack2) > 0 {
        var a, b int
        if len(stack1) > 0 {
            a = stack1[len(stack1)-1]
            stack1 = stack1[:len(stack1)-1]
        }
        if len(stack2) > 0 {
            b = stack2[len(stack2)-1]
            stack2 = stack2[:len(stack2)-1]
        }
        c := (a + b + carry) % 10
        carry = (a + b + carry) / 10
        cur := &ListNode{
            Val: c,
        }
        if head!=nil{
            cur.Next = head
        }
        head = cur
    }
    if carry > 0 {
        cur := &ListNode{
            Val: carry,
        }
        cur.Next = head
        head = cur
    }
    return head
}
发表于 2021-09-16 11:46:21 回复(0)