Skip to content

Commit 50c3538

Browse files
committedNov 25, 2022
feat: add solutions to lc problems: No.0876, 1290
- No.0876.Middle of the Linked List - No.1290.Convert Binary Number in a Linked List to Integer
1 parent 721f6b0 commit 50c3538

File tree

8 files changed

+296
-98
lines changed

8 files changed

+296
-98
lines changed
 

‎solution/0800-0899/0876.Middle of the Linked List/README.md

+49-26
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,6 @@ class Solution {
9797
}
9898
```
9999

100-
### **TypeScript**
101-
102-
```ts
103-
/**
104-
* Definition for singly-linked list.
105-
* class ListNode {
106-
* val: number
107-
* next: ListNode | null
108-
* constructor(val?: number, next?: ListNode | null) {
109-
* this.val = (val===undefined ? 0 : val)
110-
* this.next = (next===undefined ? null : next)
111-
* }
112-
* }
113-
*/
114-
115-
function middleNode(head: ListNode | null): ListNode | null {
116-
let fast = head,
117-
slow = head;
118-
while (fast != null && fast.next != null) {
119-
fast = fast.next.next;
120-
slow = slow.next;
121-
}
122-
return slow;
123-
}
124-
```
125-
126100
### **C++**
127101

128102
```cpp
@@ -168,6 +142,32 @@ func middleNode(head *ListNode) *ListNode {
168142
}
169143
```
170144

145+
### **TypeScript**
146+
147+
```ts
148+
/**
149+
* Definition for singly-linked list.
150+
* class ListNode {
151+
* val: number
152+
* next: ListNode | null
153+
* constructor(val?: number, next?: ListNode | null) {
154+
* this.val = (val===undefined ? 0 : val)
155+
* this.next = (next===undefined ? null : next)
156+
* }
157+
* }
158+
*/
159+
160+
function middleNode(head: ListNode | null): ListNode | null {
161+
let fast = head,
162+
slow = head;
163+
while (fast != null && fast.next != null) {
164+
fast = fast.next.next;
165+
slow = slow.next;
166+
}
167+
return slow;
168+
}
169+
```
170+
171171
### **Rust**
172172

173173
```rust
@@ -200,6 +200,29 @@ impl Solution {
200200
}
201201
```
202202

203+
### **C**
204+
205+
```c
206+
/**
207+
* Definition for singly-linked list.
208+
* struct ListNode {
209+
* int val;
210+
* struct ListNode *next;
211+
* };
212+
*/
213+
214+
215+
struct ListNode *middleNode(struct ListNode *head) {
216+
struct ListNode *fast = head;
217+
struct ListNode *slow = head;
218+
while (fast && fast->next) {
219+
fast = fast->next->next;
220+
slow = slow->next;
221+
}
222+
return slow;
223+
}
224+
```
225+
203226
### **...**
204227
205228
```

‎solution/0800-0899/0876.Middle of the Linked List/README_EN.md

+49-26
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,6 @@ class Solution {
7878
}
7979
```
8080

81-
### **TypeScript**
82-
83-
```ts
84-
/**
85-
* Definition for singly-linked list.
86-
* class ListNode {
87-
* val: number
88-
* next: ListNode | null
89-
* constructor(val?: number, next?: ListNode | null) {
90-
* this.val = (val===undefined ? 0 : val)
91-
* this.next = (next===undefined ? null : next)
92-
* }
93-
* }
94-
*/
95-
96-
function middleNode(head: ListNode | null): ListNode | null {
97-
let fast = head,
98-
slow = head;
99-
while (fast != null && fast.next != null) {
100-
fast = fast.next.next;
101-
slow = slow.next;
102-
}
103-
return slow;
104-
}
105-
```
106-
10781
### **C++**
10882

10983
```cpp
@@ -149,6 +123,32 @@ func middleNode(head *ListNode) *ListNode {
149123
}
150124
```
151125

126+
### **TypeScript**
127+
128+
```ts
129+
/**
130+
* Definition for singly-linked list.
131+
* class ListNode {
132+
* val: number
133+
* next: ListNode | null
134+
* constructor(val?: number, next?: ListNode | null) {
135+
* this.val = (val===undefined ? 0 : val)
136+
* this.next = (next===undefined ? null : next)
137+
* }
138+
* }
139+
*/
140+
141+
function middleNode(head: ListNode | null): ListNode | null {
142+
let fast = head,
143+
slow = head;
144+
while (fast != null && fast.next != null) {
145+
fast = fast.next.next;
146+
slow = slow.next;
147+
}
148+
return slow;
149+
}
150+
```
151+
152152
### **Rust**
153153

154154
```rust
@@ -181,6 +181,29 @@ impl Solution {
181181
}
182182
```
183183

184+
### **C**
185+
186+
```c
187+
/**
188+
* Definition for singly-linked list.
189+
* struct ListNode {
190+
* int val;
191+
* struct ListNode *next;
192+
* };
193+
*/
194+
195+
196+
struct ListNode *middleNode(struct ListNode *head) {
197+
struct ListNode *fast = head;
198+
struct ListNode *slow = head;
199+
while (fast && fast->next) {
200+
fast = fast->next->next;
201+
slow = slow->next;
202+
}
203+
return slow;
204+
}
205+
```
206+
184207
### **...**
185208
186209
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
9+
10+
struct ListNode *middleNode(struct ListNode *head) {
11+
struct ListNode *fast = head;
12+
struct ListNode *slow = head;
13+
while (fast && fast->next) {
14+
fast = fast->next->next;
15+
slow = slow->next;
16+
}
17+
return slow;
18+
}

‎solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README.md

+69-21
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ class Solution {
111111
}
112112
```
113113

114+
### **C++**
115+
116+
```cpp
117+
/**
118+
* Definition for singly-linked list.
119+
* struct ListNode {
120+
* int val;
121+
* ListNode *next;
122+
* ListNode(int x) : val(x), next(NULL) {}
123+
* };
124+
*/
125+
class Solution {
126+
public:
127+
int getDecimalValue(ListNode* head) {
128+
int res = 0;
129+
while (head != NULL) {
130+
res = (res << 1) + head->val;
131+
head = head->next;
132+
}
133+
return res;
134+
}
135+
};
136+
```
137+
114138
### **JavaScript**
115139
116140
```js
@@ -135,28 +159,30 @@ var getDecimalValue = function (head) {
135159
};
136160
```
137161

138-
### **C++**
162+
### **TypeScript**
139163

140-
```cpp
164+
```ts
141165
/**
142166
* Definition for singly-linked list.
143-
* struct ListNode {
144-
* int val;
145-
* ListNode *next;
146-
* ListNode(int x) : val(x), next(NULL) {}
147-
* };
167+
* class ListNode {
168+
* val: number
169+
* next: ListNode | null
170+
* constructor(val?: number, next?: ListNode | null) {
171+
* this.val = (val===undefined ? 0 : val)
172+
* this.next = (next===undefined ? null : next)
173+
* }
174+
* }
148175
*/
149-
class Solution {
150-
public:
151-
int getDecimalValue(ListNode* head) {
152-
int res = 0;
153-
while (head != NULL) {
154-
res = (res << 1) + head->val;
155-
head = head->next;
156-
}
157-
return res;
176+
177+
function getDecimalValue(head: ListNode | null): number {
178+
let ans = 0;
179+
let cur = head;
180+
while (cur) {
181+
ans = (ans << 1) | cur.val;
182+
cur = cur.next;
158183
}
159-
};
184+
return ans;
185+
}
160186
```
161187

162188
### **Rust**
@@ -180,15 +206,37 @@ public:
180206
// }
181207
impl Solution {
182208
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
209+
let mut ans = 0;
183210
let mut cur = &head;
184-
let mut res = 0;
185211
while let Some(node) = cur {
186-
res <<= 1;
187-
res |= node.val;
212+
ans = (ans << 1) | node.val;
188213
cur = &node.next;
189214
}
190-
res
215+
ans
216+
}
217+
}
218+
```
219+
220+
### **C**
221+
222+
```c
223+
/**
224+
* Definition for singly-linked list.
225+
* struct ListNode {
226+
* int val;
227+
* struct ListNode *next;
228+
* };
229+
*/
230+
231+
232+
int getDecimalValue(struct ListNode *head) {
233+
int ans = 0;
234+
struct ListNode *cur = head;
235+
while (cur) {
236+
ans = (ans << 1) | cur->val;
237+
cur = cur->next;
191238
}
239+
return ans;
192240
}
193241
```
194242

‎solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README_EN.md

+69-21
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ class Solution {
8181
}
8282
```
8383

84+
### **C++**
85+
86+
```cpp
87+
/**
88+
* Definition for singly-linked list.
89+
* struct ListNode {
90+
* int val;
91+
* ListNode *next;
92+
* ListNode(int x) : val(x), next(NULL) {}
93+
* };
94+
*/
95+
class Solution {
96+
public:
97+
int getDecimalValue(ListNode* head) {
98+
int res = 0;
99+
while (head != NULL) {
100+
res = (res << 1) + head->val;
101+
head = head->next;
102+
}
103+
return res;
104+
}
105+
};
106+
```
107+
84108
### **JavaScript**
85109
86110
```js
@@ -105,28 +129,30 @@ var getDecimalValue = function (head) {
105129
};
106130
```
107131

108-
### **C++**
132+
### **TypeScript**
109133

110-
```cpp
134+
```ts
111135
/**
112136
* Definition for singly-linked list.
113-
* struct ListNode {
114-
* int val;
115-
* ListNode *next;
116-
* ListNode(int x) : val(x), next(NULL) {}
117-
* };
137+
* class ListNode {
138+
* val: number
139+
* next: ListNode | null
140+
* constructor(val?: number, next?: ListNode | null) {
141+
* this.val = (val===undefined ? 0 : val)
142+
* this.next = (next===undefined ? null : next)
143+
* }
144+
* }
118145
*/
119-
class Solution {
120-
public:
121-
int getDecimalValue(ListNode* head) {
122-
int res = 0;
123-
while (head != NULL) {
124-
res = (res << 1) + head->val;
125-
head = head->next;
126-
}
127-
return res;
146+
147+
function getDecimalValue(head: ListNode | null): number {
148+
let ans = 0;
149+
let cur = head;
150+
while (cur) {
151+
ans = (ans << 1) | cur.val;
152+
cur = cur.next;
128153
}
129-
};
154+
return ans;
155+
}
130156
```
131157

132158
### **Rust**
@@ -150,15 +176,37 @@ public:
150176
// }
151177
impl Solution {
152178
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
179+
let mut ans = 0;
153180
let mut cur = &head;
154-
let mut res = 0;
155181
while let Some(node) = cur {
156-
res <<= 1;
157-
res |= node.val;
182+
ans = (ans << 1) | node.val;
158183
cur = &node.next;
159184
}
160-
res
185+
ans
186+
}
187+
}
188+
```
189+
190+
### **C**
191+
192+
```c
193+
/**
194+
* Definition for singly-linked list.
195+
* struct ListNode {
196+
* int val;
197+
* struct ListNode *next;
198+
* };
199+
*/
200+
201+
202+
int getDecimalValue(struct ListNode *head) {
203+
int ans = 0;
204+
struct ListNode *cur = head;
205+
while (cur) {
206+
ans = (ans << 1) | cur->val;
207+
cur = cur->next;
161208
}
209+
return ans;
162210
}
163211
```
164212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
9+
10+
int getDecimalValue(struct ListNode *head) {
11+
int ans = 0;
12+
struct ListNode *cur = head;
13+
while (cur) {
14+
ans = (ans << 1) | cur->val;
15+
cur = cur->next;
16+
}
17+
return ans;
18+
}

‎solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
// }
1717
impl Solution {
1818
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
19+
let mut ans = 0;
1920
let mut cur = &head;
20-
let mut res = 0;
2121
while let Some(node) = cur {
22-
res <<= 1;
23-
res |= node.val;
22+
ans = (ans << 1) | node.val;
2423
cur = &node.next;
2524
}
26-
res
25+
ans
2726
}
2827
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function getDecimalValue(head: ListNode | null): number {
14+
let ans = 0;
15+
let cur = head;
16+
while (cur) {
17+
ans = (ans << 1) | cur.val;
18+
cur = cur.next;
19+
}
20+
return ans;
21+
}

0 commit comments

Comments
 (0)
Please sign in to comment.