简体   繁体   English

从尾到头颠倒双向链接列表

[英]Reverse a Doubly Linked List From Tail to Head

I am currently practicing pointers on my school break and below I written the method to reverse a doubly linked list, but when I hand it into an online test, it fails. 我目前正在学校放假时使用指针,下面我编写了一种方法来反转双向链表,但是当我将其交给在线测试时,它失败了。

Node* Reverse(Node *head)
{
    int count = 0;
    struct Node *ptr = head;

    // return head if NULL
    if (head == NULL) {
        return head;
    }
    // if the list is only the head, then the reverse is just the head... so nothing changes
    if((head->next == NULL && head->prev == NULL)){
        return head;
    }

    //Come here if previous if statements fail, traverse the list until I reach tail which will become the
    // new head
    while(ptr->next != NULL){
        ptr = ptr->next;
        count++;
    }
    head = ptr; // this is the new head    
    //starting from tail all the way to head swap the "prev" and "next" of each node 
    struct Node *temp = ptr->next;

    for(int i = 0; i<count; i++){
        ptr->next = ptr->prev;
        ptr->prev = temp;
        ptr=ptr->next;
        temp= ptr->next;
        //count--;
    }

    return head;
}

I realize that it is probably smarter to reverse the list while I traverse it from head to tail, but I thought that was boring, so I decided to reverse it starting from the tail to head instead. 我意识到,当我从头到尾遍历列表时,反转列表可能会更聪明,但我认为这很无聊,因此我决定从尾到头进行反转。 I suspect there is an obvious error in my while loop or for loop, but I am unable to diagnose the error. 我怀疑while循环或for循环中存在明显错误,但是我无法诊断该错误。

I think the error is here: 我认为错误在这里:

while(ptr->next != NULL){
    ptr = ptr->next;
    count++;
}

Let's say your linked list has 2 elements in it. 假设您的链表中有2个元素。 Then that while loop will only iterate once, and count will be 1. When you get down to the for loop, it will also only iterate once, which means you will correctly reassign the pointers for the new head, but not the second element (previously the head). 然后, while循环将仅迭代一次,而count将为1。当您进入for循环时,它也将仅迭代一次,这意味着您将正确地为新头重新分配指针,而不是第二个元素(以前是头)。

If you initialize count to 1 instead of 0, it should correctly reflect the number of elements in the linked list and the for loop should execute correctly. 如果将count初始化为1而不是0,则它应正确反映链接列表中的元素数,并且for循环应正确执行。

Edit: You will also have to restructure your for loop slightly to avoid a segfault at the end of the list: 编辑:您还必须稍微重构for循环,以免在列表末尾出现段错误:

Node* temp;

for (int i = 0; i < count; i++)
{
    temp = ptr->next;
    ptr->next = ptr->prev;
    ptr->prev = temp;
    ptr = ptr->next;
}

replace 更换

for(int i = 0; i<count; i++){//i<count --> i<=count : Because Not counting last element
    ptr->next = ptr->prev;
    ptr->prev = temp;
    ptr=ptr->next;
    temp= ptr->next;//<-- bad
    //count--;
}

with

for(int i = 0; i <= count; i++){
    ptr->next = ptr->prev;
    ptr->prev = temp;
    temp = ptr;
    ptr = ptr->next;
}

or 要么

while(ptr){
    ptr->next = ptr->prev;
    ptr->prev = temp;
    temp = ptr;//next prev
    ptr = ptr->next;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM