1.求单链表的结点个数
struct LNode
{
int data;
LNode *next;
};
int f(LNode *head)
{
int k=0;
while(head)
{
++k;
head=head->next;
}
return k;
}
2.单链表逆序
LNode *f(LNode *head)
{
if(head==NULL||head->next==NULL)return head;
LNode *pCur=head->next,*pNext;
head->next=NULL;
while(pCur)
{
pNext=pCur->next;
pCur->next=head;
head=pCur;
pCur=pNext;
}
return head;
}
LNode *f(LNode *head)
{
if(!head||!head->next)return head;
LNode *prev=head,*cur=prev->next,*next=cur->next;
while(cur)
{
cur->next=prev;
prev=cur;
cur=next;
next=next?next->next:NULL;
}
head->next=NULL;
return prev;
}
3.求单链表倒数第k个结点
LNode *f(LNode *head,int k)
{
LNode *p=head,*q=head;
for(;k>1&&q->next;k--)q=q->next;
if(k>1)return NULL;
while(q->next)
{
p=p->next;
q=q->next;
}
return p;
}
4.求单链表的中间结点
LNode *f(LNode *head,int k)
{
LNode *p=head,*q=head;
while(q->next&&q->next->next)
{
p=p->next;
q=q->next->next;
}
return p;
}
5.单链表逆序输出
void f(LNode *head)
{
stack<int> s;
while(head)
{
s.push(head->data);
head=head->next;
}
while(!s.empty())
{
cout<<s.top()<<endl;
s.pop();
}
}
void f(LNode *head)
{
if(head==NULL)return;
f(head->next);
cout<<head->data<<endl;
}
6.合并两个有序单链表
LNode *f(LNode *head1,LNode *head2)
{
LNode *head,*pCur;
if(head1->data<=head2->data)
{
head=head1;
head1=head1->next;
}
else
{
head=head2;
head2=head2->next;
}
pCur=head;
while(head1&&head2)
{
if(head1->data<=head2->data)
{
pCur->next=head1;
pCur=head1;
head1=head1->next;
}
else
{
pCur->next=head2;
pCur=head2;
head2=head2->next;
}
}
pCur->next=head1?head1:head2;
return head;
}
7.判断两个单链表是否相交,若相交,则求相交的第一个点。
LNode *f(LNode *head1,LNode *head2)
{
LNode *p=head1,*q=head2;
int length1=1,length2=1,k;
while(p->next)
{
p=p->next;
length1++;
}
while(q->next)
{
q=q->next;
length2++;
}
if(p==q)
{
p=head1;
q=head2;
k=length1-length2;
if(length1<length2)
{
p=head2;
q=head1;
k=-k;
}
while(k--)p=p->next;
while(p)
{
if(p==q)return p;
else
{
p=p->next;
q=q->next;
}
}
}
else return NULL;
}
8.判断单链表是否有环,若有,则求环的入口点。
若slow走了s步,则fast走了2s步,设环长为r:
2s = s + nr ->
s = nr
设头结点到入口点的距离为x,入口点到相遇点的距离为y,相遇点到入口点的距离为z:
s = x + y ->
x + y = nr ->
x = nr - y ->
x = (n-1)r + z
将fast重置成表头,slow不变,两者以相同的速度走,当slow走到入口点时,fast离入口点还有(n-1)r,经过(n-1)r后两者会在入口点相遇
LNode *f(LNode *head)
{
LNode *slow=head,*fast=head;
while(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)break;
}
if(fast==NULL||fast->next==NULL)return NULL;
slow=head;
while(slow!=fast)
{
slow=slow->next;
fast=fast->next;
}
return slow;
}
9.把二元查找树转变成排序的双向链表
#include<iostream>
using namespace std;
struct node
{
int value;
node *left,*right;
node(int x):value(x),left(NULL),right(NULL){}
};
void createBinaryTree(node *&root)
{
int i;
cin>>i;
if(i==0)root=NULL;
else
{
root=new node(i);
createBinaryTree(root->left);
createBinaryTree(root->right);
}
}
void inorderTraversal(node *root,node *&tail)
{
if(root)
{
inorderTraversal(root->left,tail);
tail->right=root;
root->left=tail;
tail=root;
inorderTraversal(root->right,tail);
}
}
node *convert(node *root)
{
node dummy(-1);
node *tail=&dummy;
inorderTraversal(root,tail);
return dummy.right;
}
int main()
{
node *root;
createBinaryTree(root);
node *head=convert(root);
for(;head;head=head->right)cout<<head->value<<' ';
cout<<endl;
return 0;
}
10.Leetcode
struct node
{
int data;
node *next;
node(int x):data(x),next(NULL){}
};
//Add Two Numbers
node *addTwoNumbers(node *l1,node *l2)
{
node dummy(-1);
node *prev=&dummy;
int carry=0;
for(node *pa=l1,*pb=l2;pa||pb;pa=pa?NULL:pa->next,pb=!pb?NULL:pb->next,prev=prev->next)
{
int ai=!pa?0:pa->data;
int bi=!pb?0:pb->data;
int key=ai+bi+carry;
carry=key/10;
prev->next=new node(key%10);
}
if(carry>0)prev->next=new node(carry);
return dummy.next;
}
//Reverse Linked List II
node *reverseBetween(node *head,int m,int n)
{
node *head2=head;
for(int i=0;i<m-2;++i)head2=head2->next;
node *prev=head2->next;
node *cur=prev->next;
for(int i=m;i<n;++i)
{
prev->next=cur->next;
cur->next=prev;
head2->next=cur;
}
return head;
}
//Partition List
node *partition(node *head,int x)
{
node dummy1(-1);
node dummy2(-1);
node *cur1=&dummy1;
node *cur2=&dummy2;
for(node *cur=head;cur;cur->next)
{
if(cur->data<x)
{
cur1->next=cur;
cur1=cur;
}
else
{
cur2->next=cur;
cur2=cur;
}
}
cur1->next=dummy2.next;
cur2->next=NULL;
return dummy1.next;
}
//Remove Duplicates from Sorted List
node *deleteDuplicates(node *head)
{
if(!head)return head;
node *prev=head,*cur=head->next;
while(cur)
{
if(prev->data==cur->data)
{
prev->next=cur->next;
delete cur;
cur=prev->next;
}
else
{
prev=cur;
cur=cur->next;
}
}
return head;
}
//Remove Duplicates from Sorted List II
node *deleteDuplicates(node *head)
{
if(!head)return head;
node dummy(-1);
dummy.next=head;
node *prev=&dummy,*cur=head;
while(cur)
{
bool duplicated=false;
while(cur->next&&cur->data==cur->next->data)
{
duplicated=true;
node *tmp=cur;
cur=cur->next;
delete tmp;
}
if(duplicated)
{
node *tmp=cur;
cur=cur->next;
delete tmp;
}
else
{
prev=prev->next;
cur=cur->next;
}
}
prev->next=NULL;
return dummy.next;
}
//Rotate List
node *rotateRight(node *head,int k)
{
if(!head||k==0)return head;
int len=1;
node *p=head;
while(p->next)
{
p=p->next;
++len;
}
p->next=head;
for(k=len-k%len;k>0;--k)p=p->next;
head=p->next;
p->next=NULL;
return head;
}
//Remove Nth Node From End of List
node *removeNthFromEnd(node *head,int n)
{
node *p=head,*q=head;
for(;n>0;n--)q=q->next;
while(q->next)
{
p=p->next;
q=q->next;
}
node *tmp=p->next;
p->next=tmp->next;
delete tmp;
return head;
}
//Swap Nodes in Pairs
node *swapPairs(node *head)
{
if(!head||!head->next)return head;
node dummy(-1);
dummy.next=head;
for(node *prev=&dummy,*cur=prev->next,*next=cur->next;
next;
prev=cur,cur=next,next=next?next->next:NULL)
{
cur->next=next->next;
next->next=cur;
prev->next=next;
}
}
//Reverse Nodes in k-Group
node* reverse(node *prev,node *end)
{
node *begin=prev->next;
for(node *p=prev->next,*q=p->next,*r=q->next;
q!=end->next;
p=q,q=r,r=r?r->next:NULL)q->next=p;
begin->next=end->next;
prev->next=end;
return begin;
}
node *reverseKGroup(node *head,int k)
{
if(!head||!head->next||k<2)return head;
node dummy(-1);
dummy.next=head;
for(node *prev=&dummy,*end=head;end=prev->next;)
{
for(int i=1;i<k&&end;++i)end=end->next;
if(!end)break;
prev=reverse(prev,end);
}
}
//Reorder List
node *reverse(node *head)
{
if(!head||!head->next)return head;
node *p=head;
for(node *q=p->next,*r=q->next;
q;
p=q,q=r,r=r?r->next:NULL)q->next=p;
head->next=NULL;
return p;
}
void reorderList(node *head)
{
if(!head||!head->next||!head->next->next)return;
node *slow=head,*fast=head;
while(fast->next&&fast->next->next)
{
slow=slow->next;
fast=fast->next->next;
}
node *prev=slow->next;
slow->next=NULL;
prev=reverse(prev);
node *p=head;
while(p->next)
{
node *tmp=p->next;
p->next=prev;
prev=prev->next;
p->next->next=tmp;
p=tmp;
}
if(prev)p->next=prev;
}