mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-04 06:51:34 +00:00
链表专题更新cpp代码
This commit is contained in:
@@ -27,8 +27,13 @@
|
||||
|
||||

|
||||
|
||||
**题目代码**
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
|
||||
class Solution {
|
||||
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
|
||||
ListNode headpro = new ListNode(-1);
|
||||
ListNode headtemp = headpro;
|
||||
while (l1 != null && l2 != null) {
|
||||
@@ -45,7 +50,33 @@
|
||||
}
|
||||
headpro.next = l1 != null ? l1:l2;
|
||||
return headtemp.next;
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
C++ Code:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
|
||||
ListNode * headpro = new ListNode(-1);
|
||||
ListNode * headtemp = headpro;
|
||||
while (l1 != nullptr && l2 != nullptr) {
|
||||
//接上大的那个
|
||||
if (l1->val >= l2->val) {
|
||||
headpro->next = l2;
|
||||
l2 = l2->next;
|
||||
}
|
||||
else {
|
||||
headpro->next = l1;
|
||||
l1 = l1->next;
|
||||
}
|
||||
headpro = headpro->next;
|
||||
}
|
||||
headpro->next = l1 != nullptr ? l1: l2;
|
||||
return headtemp->next;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user