返回介绍

solution / 1200-1299 / 1290.Convert Binary Number in a Linked List to Integer / README_EN

发布于 2024-06-17 01:03:21 字数 5793 浏览 0 评论 0 收藏 0

1290. Convert Binary Number in a Linked List to Integer

中文文档

Description

Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.

Return the _decimal value_ of the number in the linked list.

The most significant bit is at the head of the linked list.

 

Example 1:

Input: head = [1,0,1]
Output: 5
Explanation: (101) in base 2 = (5) in base 10

Example 2:

Input: head = [0]
Output: 0

 

Constraints:

  • The Linked List is not empty.
  • Number of nodes will not exceed 30.
  • Each node's value is either 0 or 1.

Solutions

Solution 1

# Definition for singly-linked list.
# class ListNode:
#   def __init__(self, val=0, next=None):
#     self.val = val
#     self.next = next
class Solution:
  def getDecimalValue(self, head: ListNode) -> int:
    ans = 0
    while head:
      ans = ans << 1 | head.val
      head = head.next
    return ans
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *   int val;
 *   ListNode next;
 *   ListNode() {}
 *   ListNode(int val) { this.val = val; }
 *   ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
  public int getDecimalValue(ListNode head) {
    int ans = 0;
    for (; head != null; head = head.next) {
      ans = ans << 1 | head.val;
    }
    return ans;
  }
}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *   int val;
 *   ListNode *next;
 *   ListNode() : val(0), next(nullptr) {}
 *   ListNode(int x) : val(x), next(nullptr) {}
 *   ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
  int getDecimalValue(ListNode* head) {
    int ans = 0;
    for (; head; head = head->next) {
      ans = ans << 1 | head->val;
    }
    return ans;
  }
};
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *   Val int
 *   Next *ListNode
 * }
 */
func getDecimalValue(head *ListNode) (ans int) {
  for ; head != nil; head = head.Next {
    ans = ans<<1 | head.Val
  }
  return
}
/**
 * Definition for singly-linked list.
 * class ListNode {
 *   val: number
 *   next: ListNode | null
 *   constructor(val?: number, next?: ListNode | null) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 *   }
 * }
 */

function getDecimalValue(head: ListNode | null): number {
  let ans = 0;
  for (; head; head = head.next) {
    ans = (ans << 1) | head.val;
  }
  return ans;
}
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
//   pub val: i32,
//   pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
//   #[inline]
//   fn new(val: i32) -> Self {
//   ListNode {
//     next: None,
//     val
//   }
//   }
// }
impl Solution {
  pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
    let mut ans = 0;
    let mut cur = &head;
    while let Some(node) = cur {
      ans = (ans << 1) | node.val;
      cur = &node.next;
    }
    ans
  }
}
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *   this.val = (val===undefined ? 0 : val)
 *   this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {number}
 */
var getDecimalValue = function (head) {
  let ans = 0;
  for (; head; head = head.next) {
    ans = (ans << 1) | head.val;
  }
  return ans;
};
/**
 * Definition for a singly-linked list.
 * class ListNode {
 *   public $val = 0;
 *   public $next = null;
 *   function __construct($val = 0, $next = null) {
 *     $this->val = $val;
 *     $this->next = $next;
 *   }
 * }
 */
class Solution {
  /**
   * @param ListNode $head
   * @return Integer
   */
  function getDecimalValue($head) {
    $rs = [];
    while ($head != null) {
      array_push($rs, $head->val);
      $head = $head->next;
    }
    $rsStr = implode($rs);
    return bindec($rsStr);
  }
}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *   int val;
 *   struct ListNode *next;
 * };
 */

int getDecimalValue(struct ListNode* head) {
  int ans = 0;
  struct ListNode* cur = head;
  while (cur) {
    ans = (ans << 1) | cur->val;
    cur = cur->next;
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文