nikunjsoni

1290

Mar 16th, 2021
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Definition for singly-linked list.
  3.  * struct ListNode {
  4.  *     int val;
  5.  *     ListNode *next;
  6.  *     ListNode() : val(0), next(nullptr) {}
  7.  *     ListNode(int x) : val(x), next(nullptr) {}
  8.  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9.  * };
  10.  */
  11. class Solution {
  12. public:
  13.     int getDecimalValue(ListNode* head) {
  14.         int ans=head->val;
  15.         while(head->next != nullptr){
  16.             ans = ((ans<<1) | head->next->val);
  17.             head = head->next;
  18.         }
  19.         return ans;
  20.     }
  21. };
RAW Paste Data