当我尝试滚动列表到其最后一个元素时,为什么会遇到此错误?
我试图解决 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
函数
addNode
具有一个错误。您忘了将数据成员将NEXT
设置为null
在此代码段中,您至少需要编写
错误的原因是重复的代码。我将使用以下方式定义功能,
请注意使用两个不同的名称的同一实体的名称,例如
list
和struct listNode
使代码的读者感到困惑。要合并两个列表,无需像您在功能
MergetWolists
中所做的那样创建列表的副本。The function
addNode
has a bug. You forgot to set the data membernext
toNULL
in this code snippetYou need to write at least
The reason of the error is a duplicated code. I would define the function the following way
Pay attention to that using two different names for the same entity like
List
andstruct 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
.