【算法】1290. 二进制链表转整数(多语言实现)

82 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第4天,点击查看活动详情


1290. 二进制链表转整数:

给你一个单链表的引用结点 head。链表中每个结点的值不是 0 就是 1。已知此链表是一个整数数字的二进制表示形式。

请你返回该链表所表示数字的 十进制值

样例 1:

输入:
	head = [1,0,1]
	
输出:
	5
	
解释:
	二进制数 (101) 转化为十进制数 (5)

样例 2:

输入:
	head = [0]
	
输出:
	0

样例 3:

输入:
	head = [1]
	
输出:
	1

样例 4:

输入:
	head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
	
输出:
	18880

样例 5:

输入:
	head = [0,0]
	
输出:
	0

提示:

  • 链表不为空。
  • 链表的结点总数不超过 30。
  • 每个结点的值不是 0 就是 1。

分析

  • 面对这道算法题目,二当家的陷入了沉思。
  • 按顺序遍历链表并转换数字即可。
  • 位运算要比算术运算快。

题解

rust

// 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(mut head: Option<Box<ListNode>>) -> i32 {
        let mut ans = 0;
        while let Some(node) = head {
            ans <<= 1;
            ans |= node.val;
            head = node.next;
        }
        ans
    }
}

go

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func getDecimalValue(head *ListNode) int {
    ans := 0
    for head != nil {
        ans <<= 1
        ans |= head.Val
        head = head.Next
    }
    return ans
}

typescript

/**
 * 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;
    while (head) {
        ans <<= 1;
        ans |= head.val;
        head = head.next;
    }
    return ans;
};

c

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


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

c++

/**
 * 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;
        while (head) {
            ans <<= 1;
            ans |= head->val;
            head = head->next;
        }
        return ans;
    }
};

python

# 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 <<= 1
            ans |= head.val
            head = head.next
        return ans


java

/**
 * 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;
        while (head != null) {
            ans <<= 1;
            ans |= head.val;
            head = head.next;
        }
        return ans;
    }
}

原题传送门:https://leetcode.cn/problems/convert-binary-number-in-a-linked-list-to-integer/


非常感谢你阅读本文~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://juejin.cn/user/2771185768884824/posts 博客原创~