# Algorithm learning 14 - finding numbers in a matrix with rows and columns in order & printing the common parts of two linked lists

## Finding numbers in a matrix with ordered rows and columns

[title]

Given an integer matrix with N*M and an integer K, every row and every
The columns are all in order. Implement a function to determine whether K is in matrix.
For example:
0 1 2 5
2 3 4 7
4 4 4 8
5 7 7 9
Returns true if K is 7 and false if K is 6.
[request]
The time complexity is O(N+M), and the extra space complexity is O(1).

### Algorithmic thinking

If it is equal to k, return true to indicate that it has been found
If it is larger than k, because every column of the matrix is arranged in order, the number below the current number in the column where the current number is located is larger than k, then it is unnecessary to continue searching on col column, let col = COL-1, repeat the steps
If it's smaller than k, because every column of the matrix is arranged in order, the number to the left of the current number in the row where the current number is located is smaller than k, then it's unnecessary to continue searching on row row, make row = row + 1, and repeat the steps

### Algorithm code:

#### <isContanin.h>

```#include<iostream>
#include<vector>
using namespace std;

bool isContanin(vector<vector<int> >& matrix, int k){
int row = 0;
int col = matrix.size() - 1;
while(row < matrix.size() && col > -1)
{
if(matrix[row][col] == k)
return true;
else if(matrix[row][col] > k)
--col;
else
++row;
}
return false;
};
```

#### <isContanin.cpp>

```#include <iostream>
#include <vector>
#include"isContanin.h"
using namespace std;

int main()
{
int a[] = {0, 1, 2, 5};
vector<int> ivec(a, a + 4);
vector<vector<int> > m;
m.push_back(ivec);
ivec = 2;
ivec = 3;
ivec = 4;
ivec = 7;
m.push_back(ivec);
bool returnisContanin=isContanin(m, 7);
cout <<returnisContanin<< endl;
}
```

## Print common parts of two linked lists

[title]

### Algorithm idea:

Define two pointers, point to the head nodes of two linked lists respectively, compare the value value value, and the one who moves small; print equally and move at the same time, the next node of any node is null, and the program ends.

### code implementation

#### <printComPart.h>

```#include <iostream>

using namespace std;

struct Node
{
int value;
Node *next;
};

{
cout << "Common Part: " << endl;
{
else
{
cout << head1->value << " " ;
}
}
cout << endl;
}
```

### <printComPart.cpp>

```#include <iostream>
#include"printComPart.h"
using namespace std;

int main()
{
Node *ptr = NULL;

for(int i =0;i<10;i++)
{
{
continue;
}
ptr->next = new Node;
ptr = ptr->next;
ptr->value = i;
ptr->next = NULL;
}
for(int i =3;i<23;i++)
{
{
continue;
}
ptr->next = new Node;
ptr = ptr->next;
ptr->value = i;
ptr->next = NULL;
}  