-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathprintCommonPart.h
More file actions
48 lines (41 loc) · 1.3 KB
/
printCommonPart.h
File metadata and controls
48 lines (41 loc) · 1.3 KB
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
//
// printCommonPart.h
// linkedList
//
// Created by junl on 2019/10/27.
// Copyright © 2019 junl. All rights reserved.
//
#ifndef printCommonPart_hpp
#define printCommonPart_hpp
#include <stdio.h>
#include "creatlist.h"
/*
给定两个有序链表的头指针head1和head2,打印两个链表的公共部分
*/
namespace itinterviews {
class printCommonPart{
public:
void solve(ListNode *head1, ListNode *head2){
while (head1 && head2) {
if (head1->val == head2->val) {
std::cout << head1->val << ", ";
head1 = head1->next;
head2 = head2->next;
}else if (head1->val < head2->val){
head1 = head1->next;
}else{
head2 = head2->next;
}
}
std::cout << std::endl;
}
};
void test_printCommonPart(){
std::cout << "---给定两个有序链表的头指针head1和head2,打印两个链表的公共部分----" << std::endl;
class printCommonPart so;
ListNode *head1 = codinginterviews::creatLists({2,4,6,8})->next;
ListNode *head2 = codinginterviews::creatLists({2,3,4})->next;
so.solve(head1, head2);
}
}
#endif /* printCommonPart_hpp */