当我尝试滚动列表到其最后一个元素时,为什么会遇到此错误?

发布于 2025-02-12 09:21:17 字数 1940 浏览 1 评论 0原文

我试图解决 21。合并两个排序的列表 leetcode上。该问题的组成如下:我有两个已按顺序排列的链接列表,我必须将它们合并为单个列表,该列表也按上升顺序排序。

这是我的代码:

struct ListNode *sortList(struct ListNode *headList) {
    struct ListNode *ptrHead = headList;
    int temp;
    if(headList == NULL)
    return NULL;

    while(ptrHead->next != NULL){
        if(ptrHead->val > ptrHead->next->val){
            temp = ptrHead->val;
            ptrHead->val = ptrHead->next->val;
            ptrHead->next->val = temp;
            ptrHead = headList;
        }else{
            ptrHead = ptrHead->next;
        }
    }

    return headList;
}

void addNode(struct ListNode **list, int data){
      struct ListNode *newNode = (struct ListNode *) malloc(sizeof (struct ListNode)), *temp = NULL;

      if((*list) == NULL){
          newNode->val = data;
          newNode->next = NULL;
          (*list) = newNode;
      }else{
          temp = (*list);
          while (temp->next != NULL){
              temp = temp->next;
          }
          newNode->val = data;
          temp->next = newNode;
      }
}

struct ListNode *mergeTwoLists(struct ListNode *l1, struct ListNode *l2){
    struct ListNode *newList = NULL;

    while (l1 != NULL){
        addNode(&newList, l1->val);
        l1 = l1->next;
    }
    while (l2 != NULL){
        addNode(&newList, l2->val);
        l2 = l2->next;
    }

    return sortList(newList);
}

我尝试在本地运行它,但没有问题,但是当我在leet代码上运行它时,我会遇到错误

    Line 19: Char 20: runtime error: member access within misaligned address 
    0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment 
    [solution.c]
    0xbebebebebebebebe: note: pointer points here
    <memory cannot be printed>      

Im trying to resolve the 21. Merge Two Sorted Lists on leetCode. The problem is composed as follows: I have two linked lists already ordered in ascending order and I have to combine them into a single list that is also ordered in ascending order.

This is my code:

struct ListNode *sortList(struct ListNode *headList) {
    struct ListNode *ptrHead = headList;
    int temp;
    if(headList == NULL)
    return NULL;

    while(ptrHead->next != NULL){
        if(ptrHead->val > ptrHead->next->val){
            temp = ptrHead->val;
            ptrHead->val = ptrHead->next->val;
            ptrHead->next->val = temp;
            ptrHead = headList;
        }else{
            ptrHead = ptrHead->next;
        }
    }

    return headList;
}

void addNode(struct ListNode **list, int data){
      struct ListNode *newNode = (struct ListNode *) malloc(sizeof (struct ListNode)), *temp = NULL;

      if((*list) == NULL){
          newNode->val = data;
          newNode->next = NULL;
          (*list) = newNode;
      }else{
          temp = (*list);
          while (temp->next != NULL){
              temp = temp->next;
          }
          newNode->val = data;
          temp->next = newNode;
      }
}

struct ListNode *mergeTwoLists(struct ListNode *l1, struct ListNode *l2){
    struct ListNode *newList = NULL;

    while (l1 != NULL){
        addNode(&newList, l1->val);
        l1 = l1->next;
    }
    while (l2 != NULL){
        addNode(&newList, l2->val);
        l2 = l2->next;
    }

    return sortList(newList);
}

I tried to run it locally and I had no problem but when I go to run it on the leet code I get an error

The error:

    Line 19: Char 20: runtime error: member access within misaligned address 
    0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment 
    [solution.c]
    0xbebebebebebebebe: note: pointer points here
    <memory cannot be printed>      

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有你我更好 2025-02-19 09:21:17

函数addNode具有一个错误。您忘了将数据成员将NEXT设置为null在此代码段中,

  }else{
      temp = (*list);
      while (temp->next != NULL){
          temp = temp->next;
      }
      newNode->val = data;
      temp->next = newNode;
  }

您至少需要编写

  }else{
      temp = (*list);
      while (temp->next != NULL){
          temp = temp->next;
      }
      newNode->val = data;
      newNode->Next = NULL;
      temp->next = newNode;
  }

错误的原因是重复的代码。我将使用以下方式定义功能,

int addNode( List **list, int data )
{
    struct ListNode *newNode = malloc( sizeof ( *newNode ) );
    int success = newNode != NULL;

    if ( success )
    {
        newNode->val  = data;
        newNode->next = NULL;

        while ( *list ) list = &( *list )->next;
        *list = newNode;
    }

    return success;
}

请注意使用两个不同的名称的同一实体的名称,例如liststruct listNode使代码的读者感到困惑。

要合并两个列表,无需像您在功能MergetWolists中所做的那样创建列表的副本。

The function addNode has a bug. You forgot to set the data member next to NULL in this code snippet

  }else{
      temp = (*list);
      while (temp->next != NULL){
          temp = temp->next;
      }
      newNode->val = data;
      temp->next = newNode;
  }

You need to write at least

  }else{
      temp = (*list);
      while (temp->next != NULL){
          temp = temp->next;
      }
      newNode->val = data;
      newNode->Next = NULL;
      temp->next = newNode;
  }

The reason of the error is a duplicated code. I would define the function the following way

int addNode( List **list, int data )
{
    struct ListNode *newNode = malloc( sizeof ( *newNode ) );
    int success = newNode != NULL;

    if ( success )
    {
        newNode->val  = data;
        newNode->next = NULL;

        while ( *list ) list = &( *list )->next;
        *list = newNode;
    }

    return success;
}

Pay attention to that using two different names for the same entity like List and struct ListNode confuses readers of the code.

And to merge two lists there is no need to create copies of the lists as you are doing in the function mergeTwoLists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文