合并两个有序链表/链表反转(逆置)/找链表倒数第k点(遍历一次)

来源:互联网 发布:mac os x10.11安装 编辑:程序博客网 时间:2024/06/03 03:42
//1.【基础题】--合并两个有序链表,合并以后的链表依旧有序。 #include<iostream>using namespace std;typedef struct Node{    int data;    struct Node* next;    Node(int x)        :data(x)        ,next(NULL)    {}}Node;Node* MergeList(Node* Plist1,Node* Plist2){    if (Plist1==NULL)//如果链表1为空,返回链表2    {        return Plist2;    }    if(Plist2==NULL)//链表2为空,返回链表1    {        return Plist1;    }    if(Plist1==Plist2)//如果两个链表相等,返回任意一个     {        return Plist1;    }    //两个链表都不为空    Node* cur1=Plist1;//用于遍历链表1    Node* cur2=Plist2;//用于遍历链表2    Node* NewHead=NULL;//定义合并后新链表的头结点指针    if (Plist1->data>Plist2->data)    {        NewHead=Plist2;        cur2=cur2->next;    }    else    {        NewHead=Plist1;        cur1=cur1->next;    }    Node* NewNode=NewHead;//用于记录新链表的尾    while (cur1&&cur2)    {        if (cur1->data > cur2->data)        {           NewNode->next=cur2;           cur2=cur2->next;           NewNode=NewNode->next;//更新新链表的尾结点        }        else        {            NewNode->next=cur1;            cur1=cur1->next;            NewNode=NewNode->next;//更新新链表的尾结点        }    }    //至少有一个链表走完了    //当链表1没有走完时,将剩下的部分连接到新链表上    if (cur1)    {       NewNode->next=cur1;    }    //当链表2没有走完时,将剩下的部分连接到新链表上    if (cur2)    {        NewNode->next=cur2;    }    return NewHead;}void Printf(Node* PHead){    if (PHead==NULL)    {        return ;    }    Node* cur=PHead;    while (cur)    {        cout<<cur->data<<" ";        cur=cur->next;    }}int main(){    Node Node1(1);    Node Node2(3);    Node Node3(5);    Node Node4(7);    Node Node5(9);    Node* PHead1=&Node1;    Node1.next=&Node2;    Node2.next=&Node3;    Node3.next=&Node4;    Node4.next=&Node5;    Node Node11(2);    Node Node22(4);    Node Node33(6);    Node Node44(8);    Node Node55(10);    Node* PHead2=&Node11;    Node11.next=&Node22;    Node22.next=&Node33;    Node33.next=&Node44;    Node44.next=&Node55;    Node* NewHead=MergeList(PHead1,PHead2);    Printf(NewHead);    return 0;}----------//2.逆置/反转单链表#include<iostream>#include<assert.h>using namespace std;typedef struct Node{    int data;    struct Node* next;    Node(int x)        :data(x)        ,next(NULL)    {}}Node;Node* InverseList(Node* PHead){    //assert(PHead);    Node* NewHead=NULL;//创建一个新的头结点指针    Node* cur=PHead;//遍历原链表    while (cur)    {        Node* tmp=cur;//记录当前位置        cur=cur->next;//cur移到下一位置        tmp->next=NewHead;        NewHead=tmp;//更新头结点    }    return NewHead;}void Printf(Node* PHead){   assert(PHead);   Node* cur=PHead;   while (cur)   {       cout<<cur->data<<" ";       cur=cur->next;   }}int main(){    Node* PHead=new Node(1);    Node* Node2=new Node(2);    Node* Node3=new Node(3);    Node* Node4=new Node(4);    Node* Node5=new Node(5);    PHead->next=Node2;    Node2->next=Node3;    Node3->next=Node4;    Node4->next=Node5;    Printf(PHead);    cout<<endl;    PHead=InverseList(PHead);    Printf(PHead);    return 0;}----------//3.查找单链表的倒数第k个节点,要求只能遍历一次链表 #include<iostream>#include<assert.h>using namespace std;typedef struct Node{    int data;    struct Node* next;    Node(int x)        :data(x)        ,next(NULL)    {}}Node;Node* FindKNode(Node* PHead,int n){    assert(PHead);    Node* cur=PHead;    Node* start=PHead;    Node* end=NULL;    while (--n)    {        cur=cur->next;    }    end=cur;    while (end->next)    {        end=end->next;        start=start->next;    }    return start;}int main(){    Node* Node1=new Node(1);    Node* Node2=new Node(2);    Node* Node3=new Node(3);    Node* Node4=new Node(4);    Node* Node5=new Node(5);    Node* Node6=new Node(6);    Node* Node7=new Node(7);    Node* Node8=new Node(8);    Node1->next=Node2;     Node2->next=Node3;    Node3->next=Node4;    Node4->next=Node5;    Node5->next=Node6;    Node6->next=Node7;    Node7->next=Node8;    Node* Node=FindKNode(Node1,3);    cout<<Node->data<<endl;    return 0;}
阅读全文
0 0
原创粉丝点击