Forums Programming Assignment Help How do you write a function that can reverse a linked-list

This topic contains 1 reply, has 2 voices, and was last updated by  Aakanksha 6 years, 8 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #15428

    Pitter John
    Participant

    How do you write a function that can reverse a linked-list?

    #15430

    Aakanksha
    Participant

    answer

    void reverselist(void)
    {
    if(head==0)
    return;
    if(head->next==0)
    return;
    if(head->next==tail)
    {
    head->next = 0;
    tail->next = head;
    }
    else
    {
    node* pre = head;
    node* cur = head->next;
    node* curnext = cur->next;
    head->next = 0;
    cur-> next = head;

    for(; curnext!=0; )
    {
    cur->next = pre;
    pre = cur;
    cur = curnext;
    curnext = curnext->next;
    }

    curnext->next = cur;
    }
    }

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.