singly-linked-list 相关问题

一个链表,其中每个节点仅指向列表中的下一个节点,而不是双链表,其中每个节点都指向下一个节点和前一个节点。

为什么我达到了时间限制!? LeetCode 链表循环(已解决,但需要解释!)

我正在leetcode(https://leetcode.com/problems/linked-list-cycle/description/)上解决这个问题,我原来的解决方案超出了时间限制(示例A)。我最终能够...

回答 1 投票 0

在 C 中合并两个链表时出现问题

我应该编写一个函数来合并(将一个放在另一个的末尾)两个单链表。用户在控制台输入一串数字,例如:1 2 3 4 0(0表示结束...

回答 2 投票 0

我们可以仅使用两个指针来反转单循环链表中的元素吗?可行、高效,但时间复杂度如何?

我只需要清楚地了解一件事,因为我已经尝试过使用C语言实现单循环链表反转。因为我找不到正确的方法,而且我对此一无所知......

回答 1 投票 0

卡在Leetcode的案例3“两个和相加”

目前我正在每天进行Leetcode streak,我不知道为什么我的代码无法通过案例3。(我已经完成了代码的修改和追踪,但可能我有点笨,无法通过)找出错误...

回答 1 投票 0

用链表的哈希集去重,看不懂代码的工作流程

问题是从未排序的链表中删除重复项。使用哈希集可以跟踪该集中已有的值。 我不明白 if, else 块在做什么。 尤其是那个...

回答 1 投票 0

当我在中间引入循环时,单链表丢失的部分会发生什么?

我创建了一个单链表,显示后是这样的: 19-->85-->50-->20-->33-->9-->1-->7-->空 我创建了一个方法,可以将节点添加到...

回答 1 投票 0

当单链表中间有循环时,它的丢失部分会发生什么?

我创建了一个单链表,显示后是这样的: 19-->85-->50-->20-->33-->9-->1-->7-->空 我创建了一个方法,可以将节点添加到...

回答 2 投票 0

由于列表中间有循环,单链表的丢失部分会发生什么?

我不想通过显示整个代码来浪费您的时间。 目前我已经创建了一个单链接列表,这就是显示它后的外观。 19-->85-->50-->20-->33-->9-->...

回答 1 投票 0

Leetcode 234. 回文链表,字符串解法给出超时错误,谁能解释一下为什么吗?

这是我给出的解决方案。 类解决方案{ public boolean isPalindrome(ListNode head) { 字符串s=“”; 字符串 p=“”; 而(头!=空){ ...

回答 1 投票 0

列表节点中是否可以有链表?

不允许使用 Java 链表 API。 列表节点内是否可以有一个链表?如果是,如何将数据插入到列表节点中的链表中? 列表节点类(我试图广告...

回答 1 投票 0

删除LinkedList节点的正确方法是什么?

我已经在C中创建了一个链表。现在我想从任何位置删除节点,例如第一个节点或最后一个节点或任何第n个节点。我写了一段运行良好的代码。但问题是有人...

回答 1 投票 0

从链表中删除节点

我创建了一个带有删除方法的链表类。我已经编写了无论什么情况都删除节点的代码,例如 如果删除节点是self.head 如果

回答 1 投票 0

以反向螺旋方式打印链表

给定一个链表,任务是以螺旋方式打印一个单链表。从第一个节点开始,然后是最后一个节点,然后是第二个节点,然后是倒数第二个节点,继续...

回答 1 投票 0

链表第n次插入失败

我写了一段代码在LinkedList中插入数据。但是输出数据有问题。这是代码。 #包括 #包括 #包括 #包括 我写了一段代码来在LinkedList中插入数据。但是输出数据有问题。这是代码。 #include <stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> struct node { char *name; int age; struct node *next; }; struct node *linkeslist1head= NULL; struct node *linkedlist1tail= NULL; void llinsertend(const char *a,const int *b){ struct node *current = malloc(sizeof(struct node)); if(current == NULL){ printf("Current creation failed.\n"); } current->name = malloc(strlen(a)+1); if(current->name == NULL) { printf("String allocation failed\n"); } strcpy(current->name,a); current->age = *b; if(linkeslist1head == NULL){ linkeslist1head = current; linkedlist1tail = current; }else{ //If the list is not empty, append the new node to the end linkedlist1tail->next = current; // Update tail to point to the new last node linkedlist1tail = current; } } void llinsertbegin(const char *a, const int *b) { struct node *newnode = malloc(sizeof(struct node)); if (newnode == NULL) { printf("Memory allocation failed\n"); return; } newnode->name = malloc(strlen(a) + 1); if (newnode->name == NULL) { printf("String allocation failed\n"); free(newnode); return; } strcpy(newnode->name, a); newnode->age = *b; if (linkeslist1head == NULL) { // If the list is empty newnode->next = NULL; linkeslist1head = newnode; linkedlist1tail = newnode; } else { // If the list is not empty newnode->next = linkeslist1head; linkeslist1head = newnode; } } void llinsertaftern(const char *a, const int *b, int n) { struct node *current = linkeslist1head; int i; for (i = 1; current != NULL && i < n; i++){ current = current->next; // Iterate until the (n-1)th node or until current becomes NULL } if (current == NULL){ printf("LL short\n"); return; // Exit the function if current is NULL } printf("Reached node %d\n", i); struct node *newnode = malloc(sizeof(struct node)); if (newnode == NULL) { printf("Memory allocation failed\n"); return; } newnode->name = malloc(strlen(a) + 1); if (newnode->name == NULL) { printf("String allocation failed\n"); free(newnode); return; } strcpy(newnode->name, a); newnode->age = *b; if (current == NULL) { printf("LL is shorter than %d\n", n); free(newnode->name); free(newnode); return; } newnode->next = current->next; current->next = newnode; } void outputLinkedList(struct node *head){ struct node *p = head; while(p != NULL){ printf("Name:%s Age:%d\n",p->name,p->age); p = p->next; } printf("\n"); } int main() { printf("How many persons' details you want to add\n"); int t; scanf("%d",&t); getchar(); for(int i=1;i<=t;i++){ int x; char name[50]; scanf("%s",name); getchar(); scanf("%d",&x); llinsertend(name,&x); } int x=10,y=20,z=30; llinsertbegin("facebook",&x );//(const int *) 10 llinsertbegin("instragram", &y); llinsertbegin("whatsapp", &z); outputLinkedList(linkeslist1head); int code=1200,pos = 3; llinsertaftern("Dhaka",&code,pos); outputLinkedList(linkeslist1head); } 在第二个输出中,LinkedList 在我的 CLion 和 Codeblocks 中都不起作用,但它在编译器资源管理器中起作用。我的意思是通过 llinsertend() 函数添加一些元素然后 llinsertbegin() 输出函数完美运行。但是当我使用 llinsertaftern 函数时,它在调试器中显示分段错误。假设如果我通过 insertend 函数添加 3 个元素,并通过 insertbegin 函数添加 3 个元素,那么 pos = 3 的 inseraftern 的问题是什么。它应该工作得很好。 您的代码包含许多错误。例如,让我们考虑函数 llinsertend void llinsertend(const char *a,const int *b){ struct node *current = malloc(sizeof(struct node)); if(current == NULL){ printf("Current creation failed.\n"); } current->name = malloc(strlen(a)+1); if(current->name == NULL) { printf("String allocation failed\n"); } strcpy(current->name,a); current->age = *b; if(linkeslist1head == NULL){ linkeslist1head = current; linkedlist1tail = current; }else{ //If the list is not empty, append the new node to the end linkedlist1tail->next = current; // Update tail to point to the new last node linkedlist1tail = current; } } 首先,如果未分配新节点,则继续处理空指针 if(current == NULL){ printf("Current creation failed.\n"); } current->name = malloc(strlen(a)+1); //... 同样,如果未分配字符数组,您将再次继续使用空指针 if(current->name == NULL) { printf("String allocation failed\n"); } strcpy(current->name,a); //... 并且您忘记将新创建节点的数据成员next设置为NULL。 所以只有这一个函数包含三个错误。 注意函数llinsertaftern可以将新节点追加到列表的尾部。然而该函数不会改变指针linkedlist1tail。

回答 1 投票 0

Java:在 LinkedList 末尾插入节点

我目前正在学习Java和数据结构,我正在尝试使用插入将双精度数组中的值插入到LinkedList中,只需在列表末尾插入每个元素。我已经...

回答 1 投票 0

为什么这个数据结构会出现分段错误?

鉴于数据结构 #定义数据结构_H #定义最大索引 307 typedef enum {So, Mo, Di, Mi, Do, Fr, Sa} eDayofTheWeek; 类型定义结构{ 国际日; 月份; 年份;

回答 1 投票 0

这些链表指针声明的差异

有什么区别 struct LinkedList *current = malloc(sizeof(struct LinkedList)); 和 结构链表*当前; 在哪里 结构体链表{ 整数数据; 结构链表*下一个; } 什...

回答 2 投票 0

这些链表指针的区别

有什么区别 1struct LinkedList *current = malloc(sizeof(struct LinkedList)); 和 2struct LinkedList *当前; 在哪里 结构体链表{ 整数数据; 结构链表*下一个; } 当...

回答 1 投票 0

对链表进行排序最简单的方法

我正在尝试为链表编写非常基本的排序方法。我遇到了未处理的异常。我犯了什么错误?这是我的代码:- struct LinkedNode // 链表结构 { ...

回答 3 投票 0

CompareTo 方法:返回类型:GREATER、EQUAL、LESS

我收到以下 4 个错误: 字符预期错误 字符预期错误 我正在做一个 LinkedList java 项目,需要实现 CompareTo 方法。我对 Java 很生疏,

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.