• 链表部分逆置


    题目

    给定一个固定的单链表,输入两个数begin和end。将下标为begin到end之间的内容逆置。

    给定的单链表为:0->2->4->6->8->10->12->14->16->18

    测试数据确保begin和end不会超出单链表的长度范围,并且end>=begin

    样例输入

    2 4

    样例输出

    0->2->8->6->4->10->12->14->16->18

    分析:如果从头结点开始逆置,则将最后一个节点变为头结点,第一个节点指向尾结点后面的元素,中间的指向倒转即可。如果不是从头结点开始,还要再记录头结点前一个节点。

    代码

    #include <iostream>
    using namespace std;
    struct List
    {
        int num;
        List *next;
    };
    List *head;
    void reverse(int begin, int end, List *&head)
    {
        List *b;
        int sum=0;
        b=head;
        while(1)
        {
          if(begin==0)//需要三个指针,前两个分别指向要逆置的两个节点,第三个指向后面的一个节点
           {
               List *k=head;
               List *x=head->next,*y;
               for(int i=0;i<end-begin;i++)//循环逆置
               {
                   if(x->next!=NULL)
                       y=x->next;
                   else
                       y=NULL;
                   x->next=k;
                   k=x;
                   x=y;
               }
               head=k;
               b->next=y;
               break;
           }
           else if (sum==begin-1)//同上
           {
               List *bb=b->next;
               List *k=bb;
               List *x=bb->next,*y=x;
               for(int i=0;i<end-begin;i++)
               {
                   if(x->next!=NULL)
                   y=x->next;
                   else
                       y=NULL;
                   x->next=k;
                   k=x;
                   x=y;
               }
               b->next=k;
               bb->next=y;
               break;
           }
           else
           {
               if(b->next!=NULL){
                b=b->next;
                sum++;}
            }
           
       }
    }
    List *Create()
    {
        List *p = NULL;
        List *q = NULL;
        head = NULL;
        for ( int i = 0; i < 10; i++ ) {
            p = new List;
            p->num = i * 2;
            if ( head == NULL ) {
                head = p;
            }
            else {
                q->next = p;
            }
            q = p;
        }
    
        if ( head != NULL ) {
            q->next = NULL;
        }
    
        return head;
    }
    void displayList(List *head)
    {
        while ( head != NULL ) {
            cout << head->num;
            head = head->next;
            if ( head != NULL ) {
                cout << "->";
            }
        }
        cout << endl;
    }
    
    int main() {
        Create();
        int begin, end;
        cin >> begin >> end;
        reverse(begin, end, head);
        displayList(head);
        return 0;
    }

  • 相关阅读:
    如何使用KeyChain保存和获取UDID
    ios 使用keychain来存储token
    关于button中设置文字不显示的问题
    实现uitable cell中点击button设置当前cell为选中状态
    setImageEdgeInsets 和 setImage配合使用达到button区域大并可调节其上图片显示区域大小的效果
    C#之Action的实际应用例子
    ConcurrentQueue队列的基本使用方式
    【转】C#中base关键字的几种用法
    C# Activator和new的区别
    C#回调浅析(委托、接口回调)
  • 原文地址:https://www.cnblogs.com/nickqiao/p/7583372.html
Copyright © 2020-2023  润新知