inblog logo
|
stupefyee
    알고리즘문제풀기

    [JAVA 문제 풀이] 375. Convert Binary Number in a Linked List to Integer

    [연결된 목록의 이진수를 정수로 변환]
    Stupefyee's avatar
    Stupefyee
    Jul 14, 2025
    [JAVA 문제 풀이] 375. Convert Binary Number in a Linked List to Integer
    Contents
    내가 작성한 코드
    Convert Binary Number in a Linked List to Integer - LeetCode
    Can you solve this real interview question? Convert Binary Number in a Linked List to Integer - 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: [https://assets.leetcode.com/uploads/2019/12/05/graph-1.png] 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.
    Convert Binary Number in a Linked List to Integer - LeetCode
    https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/?envType=daily-question&envId=2025-07-14
    Convert Binary Number in a Linked List to Integer - LeetCode
    notion image
    notion image
    주어진 head는 단일 링크 목록의 참조 노드입니다. 링크된 목록의 각 노드 값은 0 또는 1입니다. 링크된 목록에는 숫자의 이진 표현이 포함되어 있습니다. 연결된 목록에 있는 숫자의 소수점 값을 반환합니다. 가장 중요한 부분은 링크된 목록의 맨 앞에 있습니다. 제약 조건: * 연결된 목록이 비어 있지 않습니다. * 노드의 수는 30개를 초과하지 않습니다. * 각 노드의 값은 0 또는 1입니다.
     

    내가 작성한 코드

    // 문제에서 제공하는 클래스 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) { StringBuilder sb = new StringBuilder(); // head의 val을 저장할 StringBuilder ListNode current = head; // 현재 노드를 가리키는 변수 while (current != null) { sb.append(current.val); // 현재 노드의 val을 StringBuilder에 추가 current = current.next; // 다음 노드로 이동 } // StringBuilder에 저장된 이진수 문자열을 10진수로 변환 int decimal = Integer.parseInt(sb.toString(), 2); return decimal; } }
    Share article
    Contents
    내가 작성한 코드

    stupefyee

    RSS·Powered by Inblog