#include <iostream>
using namespace std;
struct ListNode
{
ListNode * next;
int data;
};
//find the middle node in the linked list
ListNode * findMid(ListNode *phead)
{
ListNode * pslow=phead;
ListNode * pquick=phead;
while( pquick->next->next)// the last second is not visited; after this process the "pquick" point to the last second;
{
pslow = pslow->next;
pquick = pquick->next->next;
}
cout <<"pquick->data "<< pquick->data<<endl;
return pslow;
}
int main()
{
ListNode * head = new ListNode[10];
for(int i = 0;i<9;++i)
{
head[i].next = &head[i+1];
head[i].data = i;
}
head[9].data = 9;
head[9].next = 0;
ListNode * ph = head;
while(ph->next)//while(ph->next) the last is not visited ; after this process the ph point to the last one;
{
cout << ph->data<<endl;
ph=ph->next;
}
while(ph)// visit all the nodes; after this process the ph is null ; after the process point to the last -1 one; means null;
{
cout << ph->data<<endl;
ph=ph->next;
}
cout << "ph->data = "<<ph->data<<endl;
cout <<"middle is"<< findMid(head)->data <<endl;
return 0;
}
how to garantee the p->next->next is correct ?
tow extreme condition :
1 = the p points to the last node;
2 = the p points to the last second node;
in the two conditions :
it will cause the wrong problem ;
so the two conditions can not be happened !! at the same time !!!
so
p->next !=null (1)
&&
p->next->next !=null (2)
so we must write them as the while condition ;
2
#include <iostream>
using namespace std;
struct ListNode
{
ListNode * next;
int data;
};
//find the middle node in the linked list
ListNode * findMid(ListNode *phead)
{
if(phead->next->next == NULL || phead->next == NULL || phead==NULL)
{
return phead;
}
ListNode * pslow=phead;
ListNode * pquick=phead;
while(pquick->next && pquick->next->next)// the last second is not visited; after this process the "pquick" point to the last second;
{
pslow = pslow->next;
pquick = pquick->next->next;
}
cout <<"pquick->data "<< pquick->data<<endl;
return pslow;
}
void showList(ListNode *phead)
{
ListNode *ph = phead;
while(ph)
{
cout << ph->data<<endl;
ph=ph->next;
}
}
ListNode *Merge(ListNode *ph1,ListNode *ph2)
{
ListNode *res = ph1->data < ph2->data ? ph1 : ph2;
ListNode *p1 = ph1;
ListNode *pp1 = 0;//store for p1;
ListNode *p2 = ph2;
ListNode *pp2 = 0;//store for p2;
while(1)
{
while(p1->data<p2->data &&p1->next!=NULL)
{
pp1=p1;
p1=p1->next;
}
if(p1->next==NULL)
{
if(p1->data<p2->data)
{
p1->next=p2;
return res;
}
else
{
pp1->next=p2;
}
}
// when the conditon is violated by the first condition
// p1->data<p2->data; the system should do the following ~~
while(p2->data<p1->data &&p2->next!=NULL)
{
pp2=p2;
p2=p2->next;
}
if(p2->next==NULL)
{
if(p2->data<p1->data)
{
p2->next=p1;
return res;
}
else
{
pp2->next=p1;
}
}
}
}
int main()
{
ListNode * head = new ListNode[10];
int k = -1;
for(int i = 0;i<9;++i)
{
head[i].next = &head[i+1];
head[i].data = i*2;
// k=-k;
}
//head[0].data = -1;
head[9].data = 18;
head[9].next = 0;
ListNode * head1 = new ListNode[10];
for(int j=0;j<9;++j)
{
head1[j].next = &head1[j+1];
head1[j].data = j*2+1;
}
head1[9].data = 19;
head1[9].next = 0;
cout<<"Noded list one"<<endl;
showList(head);
cout << "Linked list two"<<endl;
showList(head1);
cout <<"Merge"<<endl;
showList(Merge(head,head1));
//cout << findMid(head)->data<<endl;
return 0;
}