File tree Expand file tree Collapse file tree 6 files changed +94
-66
lines changed
solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer Expand file tree Collapse file tree 6 files changed +94
-66
lines changed Original file line number Diff line number Diff line change 7676
7777class Solution :
7878 def getDecimalValue (self , head : ListNode) -> int :
79- n, cur = 0 , head
80- while cur:
81- n += 1
82- cur = cur.next
83- res, cur = 0 , head
84- while cur:
85- n -= 1
86- if cur.val == 1 :
87- res += (2 ** (n))
88- cur = cur.next
79+ res = 0
80+ while head:
81+ res = (res << 1 ) + head.val
82+ head = head.next
8983 return res
9084```
9185
@@ -104,16 +98,10 @@ class Solution:
10498 */
10599class Solution {
106100 public int getDecimalValue (ListNode head ) {
107- int n = 0 ;
108- for (ListNode cur = head; cur != null ; cur = cur. next) {
109- ++ n;
110- }
111101 int res = 0 ;
112- for (ListNode cur = head; cur != null ; cur = cur. next) {
113- -- n;
114- if (cur. val == 1 ) {
115- res += Math . pow(2 , n);
116- }
102+ while (head != null ) {
103+ res = (res << 1 ) + head. val;
104+ head = head. next;
117105 }
118106 return res;
119107 }
@@ -136,15 +124,38 @@ class Solution {
136124 */
137125var getDecimalValue = function (head ) {
138126 let res = 0 ;
139- while (head !== null ) {
140- res *= 2 ;
141- if (head .val ) res += 1 ;
127+ while (head != null ) {
128+ res = (res << 1 ) + head .val ;
142129 head = head .next ;
143130 }
144131 return res;
145132};
146133```
147134
135+ ### ** C++**
136+
137+ ``` cpp
138+ /* *
139+ * Definition for singly-linked list.
140+ * struct ListNode {
141+ * int val;
142+ * ListNode *next;
143+ * ListNode(int x) : val(x), next(NULL) {}
144+ * };
145+ */
146+ class Solution {
147+ public:
148+ int getDecimalValue(ListNode* head) {
149+ int res = 0;
150+ while (head != NULL) {
151+ res = (res << 1) + head->val;
152+ head = head->next;
153+ }
154+ return res;
155+ }
156+ };
157+ ```
158+
148159### **...**
149160
150161```
Original file line number Diff line number Diff line change 8989
9090class Solution :
9191 def getDecimalValue (self , head : ListNode) -> int :
92- n, cur = 0 , head
93- while cur:
94- n += 1
95- cur = cur.next
96- res, cur = 0 , head
97- while cur:
98- n -= 1
99- if cur.val == 1 :
100- res += (2 ** (n))
101- cur = cur.next
92+ res = 0
93+ while head:
94+ res = (res << 1 ) + head.val
95+ head = head.next
10296 return res
10397```
10498
@@ -115,16 +109,10 @@ class Solution:
115109 */
116110class Solution {
117111 public int getDecimalValue (ListNode head ) {
118- int n = 0 ;
119- for (ListNode cur = head; cur != null ; cur = cur. next) {
120- ++ n;
121- }
122112 int res = 0 ;
123- for (ListNode cur = head; cur != null ; cur = cur. next) {
124- -- n;
125- if (cur. val == 1 ) {
126- res += Math . pow(2 , n);
127- }
113+ while (head != null ) {
114+ res = (res << 1 ) + head. val;
115+ head = head. next;
128116 }
129117 return res;
130118 }
@@ -147,15 +135,38 @@ class Solution {
147135 */
148136var getDecimalValue = function (head ) {
149137 let res = 0 ;
150- while (head !== null ) {
151- res *= 2 ;
152- if (head .val ) res += 1 ;
138+ while (head != null ) {
139+ res = (res << 1 ) + head .val ;
153140 head = head .next ;
154141 }
155142 return res;
156143};
157144```
158145
146+ ### ** C++**
147+
148+ ``` cpp
149+ /* *
150+ * Definition for singly-linked list.
151+ * struct ListNode {
152+ * int val;
153+ * ListNode *next;
154+ * ListNode(int x) : val(x), next(NULL) {}
155+ * };
156+ */
157+ class Solution {
158+ public:
159+ int getDecimalValue(ListNode* head) {
160+ int res = 0;
161+ while (head != NULL) {
162+ res = (res << 1) + head->val;
163+ head = head->next;
164+ }
165+ return res;
166+ }
167+ };
168+ ```
169+
159170### **...**
160171
161172```
Original file line number Diff line number Diff line change 1+ /* *
2+ * Definition for singly-linked list.
3+ * struct ListNode {
4+ * int val;
5+ * ListNode *next;
6+ * ListNode(int x) : val(x), next(NULL) {}
7+ * };
8+ */
9+ class Solution {
10+ public:
11+ int getDecimalValue (ListNode* head) {
12+ int res = 0 ;
13+ while (head != NULL ) {
14+ res = (res << 1 ) + head->val ;
15+ head = head->next ;
16+ }
17+ return res;
18+ }
19+ };
Original file line number Diff line number Diff line change 88 */
99class Solution {
1010 public int getDecimalValue (ListNode head ) {
11- int n = 0 ;
12- for (ListNode cur = head ; cur != null ; cur = cur .next ) {
13- ++n ;
14- }
1511 int res = 0 ;
16- for (ListNode cur = head ; cur != null ; cur = cur .next ) {
17- --n ;
18- if (cur .val == 1 ) {
19- res += Math .pow (2 , n );
20- }
12+ while (head != null ) {
13+ res = (res << 1 ) + head .val ;
14+ head = head .next ;
2115 }
2216 return res ;
2317 }
Original file line number Diff line number Diff line change 1111 */
1212 var getDecimalValue = function ( head ) {
1313 let res = 0 ;
14- while ( head !== null ) {
15- res *= 2 ;
16- if ( head . val ) res += 1 ;
14+ while ( head != null ) {
15+ res = ( res << 1 ) + head . val ;
1716 head = head . next ;
1817 }
1918 return res ;
Original file line number Diff line number Diff line change 66
77class Solution :
88 def getDecimalValue (self , head : ListNode ) -> int :
9- n , cur = 0 , head
10- while cur :
11- n += 1
12- cur = cur .next
13- res , cur = 0 , head
14- while cur :
15- n -= 1
16- if cur .val == 1 :
17- res += (2 ** (n ))
18- cur = cur .next
9+ res = 0
10+ while head :
11+ res = (res << 1 ) + head .val
12+ head = head .next
1913 return res
You can’t perform that action at this time.
0 commit comments