|
53 | 53 |
|
54 | 54 | <!-- 这里可写通用的实现逻辑 -->
|
55 | 55 |
|
56 |
| -**方法一:栈** |
| 56 | +**方法一:翻转** |
57 | 57 |
|
58 |
| -利用两个栈分别存储两个链表元素。然后对两个栈中对应元素相加,并记录进位 carry。 |
| 58 | +手动翻转链表 `l1` 与 `l2`,将此题转换为 [2. 两数相加](https://leetcode.cn/problems/add-two-numbers/),相加过程一致。对于最后返回的结果链表也需要进行翻转,共计三次。 |
| 59 | + |
| 60 | +**方法二:栈** |
| 61 | + |
| 62 | +进阶条件限制,不可翻转。可以利用两个栈,分别存储两个链表元素。然后对两个栈中对应元素相加,并记录进位 carry。 |
59 | 63 |
|
60 | 64 | <!-- tabs:start -->
|
61 | 65 |
|
@@ -209,6 +213,209 @@ func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
209 | 213 | }
|
210 | 214 | ```
|
211 | 215 |
|
| 216 | +### **TypeScript** |
| 217 | + |
| 218 | +```ts |
| 219 | +/** |
| 220 | + * Definition for singly-linked list. |
| 221 | + * class ListNode { |
| 222 | + * val: number |
| 223 | + * next: ListNode | null |
| 224 | + * constructor(val?: number, next?: ListNode | null) { |
| 225 | + * this.val = (val===undefined ? 0 : val) |
| 226 | + * this.next = (next===undefined ? null : next) |
| 227 | + * } |
| 228 | + * } |
| 229 | + */ |
| 230 | + |
| 231 | +const reverse = (head: ListNode | null) => { |
| 232 | + let pre = null; |
| 233 | + while (head != null) { |
| 234 | + const { next } = head; |
| 235 | + head.next = pre; |
| 236 | + pre = head; |
| 237 | + head = next; |
| 238 | + } |
| 239 | + return pre; |
| 240 | +}; |
| 241 | + |
| 242 | +function addTwoNumbers( |
| 243 | + l1: ListNode | null, |
| 244 | + l2: ListNode | null, |
| 245 | +): ListNode | null { |
| 246 | + l1 = reverse(l1); |
| 247 | + l2 = reverse(l2); |
| 248 | + const dummy = new ListNode(); |
| 249 | + let cur = dummy; |
| 250 | + let sum = 0; |
| 251 | + while (l1 != null || l2 != null || sum !== 0) { |
| 252 | + if (l1 != null) { |
| 253 | + sum += l1.val; |
| 254 | + l1 = l1.next; |
| 255 | + } |
| 256 | + if (l2 != null) { |
| 257 | + sum += l2.val; |
| 258 | + l2 = l2.next; |
| 259 | + } |
| 260 | + cur.next = new ListNode(sum % 10); |
| 261 | + cur = cur.next; |
| 262 | + sum = Math.floor(sum / 10); |
| 263 | + } |
| 264 | + return reverse(dummy.next); |
| 265 | +} |
| 266 | +``` |
| 267 | + |
| 268 | +```ts |
| 269 | +/** |
| 270 | + * Definition for singly-linked list. |
| 271 | + * class ListNode { |
| 272 | + * val: number |
| 273 | + * next: ListNode | null |
| 274 | + * constructor(val?: number, next?: ListNode | null) { |
| 275 | + * this.val = (val===undefined ? 0 : val) |
| 276 | + * this.next = (next===undefined ? null : next) |
| 277 | + * } |
| 278 | + * } |
| 279 | + */ |
| 280 | + |
| 281 | +const createStack = (head: ListNode | null) => { |
| 282 | + const res = []; |
| 283 | + while (head != null) { |
| 284 | + res.push(head.val); |
| 285 | + head = head.next; |
| 286 | + } |
| 287 | + return res; |
| 288 | +}; |
| 289 | + |
| 290 | +function addTwoNumbers( |
| 291 | + l1: ListNode | null, |
| 292 | + l2: ListNode | null, |
| 293 | +): ListNode | null { |
| 294 | + const stack1 = createStack(l1); |
| 295 | + const stack2 = createStack(l2); |
| 296 | + const dummy = new ListNode(); |
| 297 | + let sum = 0; |
| 298 | + while (stack1.length !== 0 || stack2.length !== 0 || sum !== 0) { |
| 299 | + sum += (stack1.pop() ?? 0) + (stack2.pop() ?? 0); |
| 300 | + dummy.next = new ListNode(sum % 10, dummy.next); |
| 301 | + sum = Math.floor(sum / 10); |
| 302 | + } |
| 303 | + return dummy.next; |
| 304 | +} |
| 305 | +``` |
| 306 | + |
| 307 | +### **Rust** |
| 308 | + |
| 309 | +```rust |
| 310 | +// Definition for singly-linked list. |
| 311 | +// #[derive(PartialEq, Eq, Clone, Debug)] |
| 312 | +// pub struct ListNode { |
| 313 | +// pub val: i32, |
| 314 | +// pub next: Option<Box<ListNode>> |
| 315 | +// } |
| 316 | +// |
| 317 | +// impl ListNode { |
| 318 | +// #[inline] |
| 319 | +// fn new(val: i32) -> Self { |
| 320 | +// ListNode { |
| 321 | +// next: None, |
| 322 | +// val |
| 323 | +// } |
| 324 | +// } |
| 325 | +// } |
| 326 | +impl Solution { |
| 327 | + fn reverse(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { |
| 328 | + let mut pre = None; |
| 329 | + while let Some(mut node) = head { |
| 330 | + let next = node.next.take(); |
| 331 | + node.next = pre.take(); |
| 332 | + pre = Some(node); |
| 333 | + head = next; |
| 334 | + } |
| 335 | + pre |
| 336 | + } |
| 337 | + |
| 338 | + pub fn add_two_numbers( |
| 339 | + mut l1: Option<Box<ListNode>>, |
| 340 | + mut l2: Option<Box<ListNode>>, |
| 341 | + ) -> Option<Box<ListNode>> { |
| 342 | + l1 = Self::reverse(l1); |
| 343 | + l2 = Self::reverse(l2); |
| 344 | + let mut dummy = Some(Box::new(ListNode::new(0))); |
| 345 | + let mut cur = &mut dummy; |
| 346 | + let mut sum = 0; |
| 347 | + while l1.is_some() || l2.is_some() || sum != 0 { |
| 348 | + if let Some(node) = l1 { |
| 349 | + sum += node.val; |
| 350 | + l1 = node.next; |
| 351 | + } |
| 352 | + if let Some(node) = l2 { |
| 353 | + sum += node.val; |
| 354 | + l2 = node.next; |
| 355 | + } |
| 356 | + cur.as_mut().unwrap().next = Some(Box::new(ListNode::new(sum % 10))); |
| 357 | + cur = &mut cur.as_mut().unwrap().next; |
| 358 | + sum /= 10; |
| 359 | + } |
| 360 | + Self::reverse(dummy.unwrap().next.take()) |
| 361 | + } |
| 362 | +} |
| 363 | +``` |
| 364 | + |
| 365 | +```rust |
| 366 | +// Definition for singly-linked list. |
| 367 | +// #[derive(PartialEq, Eq, Clone, Debug)] |
| 368 | +// pub struct ListNode { |
| 369 | +// pub val: i32, |
| 370 | +// pub next: Option<Box<ListNode>> |
| 371 | +// } |
| 372 | +// |
| 373 | +// impl ListNode { |
| 374 | +// #[inline] |
| 375 | +// fn new(val: i32) -> Self { |
| 376 | +// ListNode { |
| 377 | +// next: None, |
| 378 | +// val |
| 379 | +// } |
| 380 | +// } |
| 381 | +// } |
| 382 | +impl Solution { |
| 383 | + fn create_stack(mut head: Option<Box<ListNode>>) -> Vec<i32> { |
| 384 | + let mut res = vec![]; |
| 385 | + while let Some(node) = head { |
| 386 | + res.push(node.val); |
| 387 | + head = node.next; |
| 388 | + } |
| 389 | + res |
| 390 | + } |
| 391 | + |
| 392 | + pub fn add_two_numbers( |
| 393 | + l1: Option<Box<ListNode>>, |
| 394 | + l2: Option<Box<ListNode>>, |
| 395 | + ) -> Option<Box<ListNode>> { |
| 396 | + let mut stack1 = Self::create_stack(l1); |
| 397 | + let mut stack2 = Self::create_stack(l2); |
| 398 | + |
| 399 | + let mut dummy = Box::new(ListNode::new(0)); |
| 400 | + let mut sum = 0; |
| 401 | + while !stack1.is_empty() || !stack2.is_empty() || sum != 0 { |
| 402 | + if let Some(val) = stack1.pop() { |
| 403 | + sum += val; |
| 404 | + } |
| 405 | + if let Some(val) = stack2.pop() { |
| 406 | + sum += val; |
| 407 | + } |
| 408 | + dummy.next = Some(Box::new(ListNode { |
| 409 | + val: sum % 10, |
| 410 | + next: dummy.next.take(), |
| 411 | + })); |
| 412 | + sum /= 10; |
| 413 | + } |
| 414 | + dummy.next.take() |
| 415 | + } |
| 416 | +} |
| 417 | +``` |
| 418 | + |
212 | 419 | ### **...**
|
213 | 420 |
|
214 | 421 | ```
|
|
0 commit comments