You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */funcgetDecimalValue(head*ListNode) int {
sum:=0forhead!=nil {
sum=sum*2+head.Valhead=head.Next
}
returnsum
}
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */funcgetDecimalValue(head*ListNode) int {
ifhead==nil {
return0
}
vars []intforhead!=nil {
s=append(s, head.Val)
head=head.Next
}
n:=len(s)
length:=len(s)
sum:=0forn>0 {
ifs[n-1] ==1 {
sum+=int(math.Pow(2, float64(length-n)))
}
n--
}
returnsum
}
The text was updated successfully, but these errors were encountered:
给你一个单链表的引用结点 head。链表中每个结点的值不是 0 就是 1。已知此链表是一个整数数字的二进制表示形式。
请你返回该链表所表示数字的 十进制值 。
示例 1:
示例 2:
示例 3:
示例 4:
示例 5:
提示:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/convert-binary-number-in-a-linked-list-to-integer
The text was updated successfully, but these errors were encountered: