Convert Binary Number in a Linked List to Integer LeetCode solution in Java
1/**2* Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode() {}3* ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val;4* this.next = next; } }5*/6class Solution {7public int getDecimalValue(ListNode head) {8head = reverse(head);9int ans = 0;10int pow = 0;11ListNode temp = head;12while (temp != null) {13ans = ans + temp.val * (int) Math.pow(2, pow++);14temp = temp.next;15}1617return ans;18}1920public ListNode reverse(ListNode head) {21ListNode prev = null;22ListNode pres = head;23ListNode Next = pres.next;2425while (pres != null) {26pres.next = prev;27prev = pres;28pres = Next;29if (Next != null) {30Next = Next.next;31}32}3334head = prev;35return head;36}37}