【Leetcode】1290. Convert Binary Number in a Linked List to Integer

题目地址:

https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/

给定一个只由 0 0 0 1 1 1构成的链表,返回其代表的二进制数。直接模拟就好了。代码如下:

public class Solution {
     
    public int getDecimalValue(ListNode head) {
     
        int res = 0;
        while (head != null) {
     
            res <<= 1;
            res += head.val;
            head = head.next;
        }
        
        return res;
    }
}

class ListNode {
     
    int val;
    ListNode next;
    ListNode(int x) {
     
        val = x;
    }
}

时间复杂度 O ( n ) O(n) O(n)

你可能感兴趣的:(#,数组,链表与模拟,链表,leetcode,java,算法)