Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
bluetch
leetcode-javascript
Repository navigation
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
leetcode-javascript
/
algorithms
/
21-merge-two-sorted-lists.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
56 lines (47 loc) · 1.78 KB
main
Breadcrumbs
leetcode-javascript
/
algorithms
/
21-merge-two-sorted-lists.js
Copy path
Top
File metadata and controls
Code
Blame
56 lines (47 loc) · 1.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Problem: https://leetcode.com/problems/merge-two-sorted-lists/
* Difficulty: Easy
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* Solution: https://ithelp.ithome.com.tw/articles/10246209
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/
function ListNode(val, next) {
this.val = val;
this.next = next;
}
let mergeTwoLists = (l1, l2) => {
// 創建一個暫時的node作為串列的起始點
let current = new ListNode(0);
// 將最後需回傳的串列指向這個ListNode物件
let returnList = current;
// sum為加總,carry用來處理進位值
let sum,
carry = 0;
// 判斷l1和l2是否為null(即遍歷到串列的尾部節點了),或carry有進位值待處理
while (l1 || l2 || carry > 0) {
sum = 0; // init sum
if (l1 !== null) {
sum += l1.val;
l1 = l1.next; // l1作為變數,其指向替換為下一個節點
}
if (l2 !== null) {
sum += l2.val;
l2 = l2.next; // l2作為變數,其指向替換為下一個節點
}
sum += carry; // 記得加上carry進位
// 第一次迴圈current代表開始時new ListNode(0)物件,利用%計算餘數並將next指向這個新建的node
current.next = new ListNode(sum % 10);
// 記得current只是一個變數,此處將current指向上述新建的node,第二次回圈後就可以進行上述同樣的操作
current = current.next;
// carry count 若大於9則進位
carry = sum > 9 ? 1 : 0;
}
// 最後returnList指向的ListNode(0)物件會因上式不斷的增加新節點,故我們只需回傳剔除ListNode(0)的串列,也就是next所指向的node
return returnList.next;
};
You can’t perform that action at this time.