Skip to content

Commit 50c3538

Browse files
committed
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

0 commit comments

Comments
 (0)