题解 | #给单链表加一#

给单链表加一

http://www.nowcoder.com/practice/a2f1105d2ac5466e9ba8fd61310ba6d1

package main import . "nc_tools" /*

  • type ListNode struct{
  • Val int
  • Next *ListNode
  • } */

/**

  • 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
  • @param head ListNode类
  • @return ListNode类 */ func plusOne( head *ListNode ) *ListNode { // write code here dumny := &ListNode{Next: head} root := f(dumny) if root.Val > 0 { return root }else{ return root.Next } }

//后序遍历 func f(head *ListNode) *ListNode { // write code here if head.Next == nil { head.Val += 1 return head } next := f(head.Next) if next.Val > 9 { head.Val+= 1 next.Val = next.Val - 10 } return head }

全部评论

相关推荐

大叔叔1:你把自己说的话打码,所以你想表达什么
点赞 评论 收藏
分享
牛客464620405号:随便投,随便找,中国经过40多年的改革开放,人才缺口和职位空缺是巨大的,中国现在属于遍地黄金的年代,属于90后和00大机遇的时代
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务