回覆列表
  • 1 # 使用者1062846289535

    typedef struct ListNode{

    ListNode *next;

    Element data;

    }ListNode, *pList;

    這是我做的單鏈表逆序三個不同的演算法,2個遞迴的以及一個非遞迴的

    pList ReverseList( pList head ){

    if( !head || !(head->next) )

    return head;

    pList ph = ReverseList( head->next );

    head->next->next = head;

    head->next = NULL;

    return ph;

    }

    pList ReverseList( pList head , pList &tail ){

    if( !head || !(head->next) ){

    tail = head;

    return head;

    }

    pList pt;

    pList ph = ReverseList( head->next , pt );

    pt->next = head;

    head->next = NULL;

    tail = head;

    return ph;

    }

    pList ReverseListNonRec( pList head ){

    if( !head || !(head->next) )

    return head;

    pList h = NULL,h1 = head;

    while( head ){

    h1 = head->next;

    head->next = h;

    h = head;

    head = h1;

    }

    return h;

    }

  • 中秋節和大豐收的關聯?
  • 如何批評員工,使員工能夠不反感並且接受批評?