在不修改头指针的情况下反转链表

时间:2015-05-21 05:26:34

标签: c pointers reverse palindrome

我正在编写一个程序来检查单链表是否是回文。 我的方法如下:

  1. 创建单链表
  2. 反转列表并将其存储在另一个变量
  3. 比较两者。
  4. 反向操作修改头指针。为了不修改头指针,我创建了一个temp变量来指向头并用相同的方式反转列表并返回temp指针。操作如下:

    struct node* reverse()
    {
        struct node *temp=head;
        struct node *curr=temp, *prev=NULL, *next=NULL;
        while(curr!=NULL)
        {
              next = curr->next;
            curr->next = prev;
            prev = curr;
            curr = next;
        }
        temp = prev;
        return temp;
    }
    

    现在,我的回文功能实现如下:

    int isPalindrome()
    {
        struct node *temp=head, *rev=head;
        rev = reverse();
        while(temp!=NULL)
        {
            if(temp->data != rev->data)
                return 0;
            temp = temp->next;
            rev = rev->next;
        }
        return 1;
    }
    

    如果反转列表与原始列表节点不匹配,则返回0.否则返回1.

    它适用于一些测试用例。但是当列表是1-> 4-> 3-> 2-> 1时。它返回1但它应该返回0.

    在锻炼时,我发现,rev函数修改了头部指针。现在,我的列表只有一个节点。

    输出如下所示:
    enter image description here

    1. 第一个列表是使用头指针未修改
    2. 进行打印
    3. 第二个列表是反向列表
    4. 第三个列表是在调用reverse函数
    5. 后打印原始列表

2 个答案:

答案 0 :(得分:3)

将原始头部反转到反转列表中的最后一个元素后,这就是为什么在第3个输出中只看到一个元素的原因。有几个选项:

  1. 使用双链表。从头部和尾部移动,同时朝相反方向移动。

  2. 在倒车前复制原始列表并比较两个列表。

  3. 使用堆栈或其他一些数据结构来提供帮助(类似于上面的选项2)。

  4. 修改算法以仅使用原始列表。

答案 1 :(得分:1)

很明显,函数reverse是错误的,因为它应该构建一个新列表。这意味着它必须基于原始列表的节点为反向列表创建新节点。也就是说,它必须使用标准函数malloc在内存中分配其节点。

定义一个比较两个列表的函数也更符合逻辑。使用此功能,您可以说列表是否是回文。

这是一个演示程序,您可以将其用作自己程序的示例。

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *next;
};

void push_front( struct node **head, int data )
{
    struct node *n = malloc( sizeof( struct node ) );

    n->data = data;
    n->next = *head;

    *head = n;
}

void display( struct node *head )
{
    for ( ; head != NULL; head = head->next )
    {
        printf( "%d ", head->data );
    }
    printf( "\n" );
}

struct node * reverse_copy( struct node *head )
{
    struct node *rev_head = NULL;

    for ( ; head != NULL; head = head->next )
    {
        push_front( &rev_head, head->data ); 
    }

    return rev_head;
}

int equal( struct node *head1, struct node *head2 )
{
    while ( head1 != NULL && head2 != NULL && head1->data == head2->data )
    {
        head1 = head1->next;
        head2 = head2->next;
    }

    return head1 == NULL && head2 == NULL;
}

void free_list( struct node **head )
{
    struct node *n = *head;

    while ( n != NULL )
    {
        struct node *tmp = n;
        n = n->next;
        free( tmp );
    }

    *head = NULL;
}

struct node *head;

int main(void) 
{
    push_front( &head, 1 );
    push_front( &head, 2 );
    push_front( &head, 3 );
    push_front( &head, 4 );
    push_front( &head, 1 );

    display( head );

    struct node *rev_head = reverse_copy( head );

    if ( equal( head, rev_head ) )
    {
        printf( "The list is a palindrome\n" );
    }
    else
    {
        printf( "The list is not a palindrome\n" );
    }

    free_list( &head );
    free_list( &rev_head );

    push_front( &head, 1 );
    push_front( &head, 2 );
    push_front( &head, 3 );
    push_front( &head, 2 );
    push_front( &head, 1 );

    display( head );

    rev_head = reverse_copy( head );

    if ( equal( head, rev_head ) )
    {
        printf( "The list is a palindrome\n" );
    }
    else
    {
        printf( "The list is not a palindrome\n" );
    }

    free_list( &head );
    free_list( &rev_head );

    return 0;
}

程序输出

1 4 3 2 1 
The list is not a palindrome
1 2 3 2 1 
The list is a palindrome
相关问题