chapter_array_and_linkedlist/linked_list/ #83
Replies: 123 comments 199 replies
-
请教k大,在fig.链表定义与存储方式图中,浅蓝色的存储结点指针是占用一块内存地址吗?还是和结点值各占一半呢?谢谢解答。 |
Beta Was this translation helpful? Give feedback.
-
请教k大!在看图Fig. 链表定义与存储方式和下面的代码时,感觉有些奇怪,这两个描述的是不是不是一个层面,在java中一切都是对象,那绿色的值的节点存的也是指针吧,那他们一个是内存层面一个是代码层面,不是一个层面的数据结构为什么能直接运用到java的对象中呢? |
Beta Was this translation helpful? Give feedback.
-
k神链表常用操作里,“输出结点在链表中的索引”,代码都是return -1, 是不是应该return index? |
Beta Was this translation helpful? Give feedback.
-
链表访问节点处的代码的for循环中,先令 head = head.next 再判断 head 是否为空,请问一下先判断head.next 是否为空,若为空则返回 null ;不为空再令 head = head.next 会不会好一点点。 |
Beta Was this translation helpful? Give feedback.
-
想问一下 链表的插入和删除确实是O(1) 但是查找既然是需要遍历。那应该是O(n)不是么? |
Beta Was this translation helpful? Give feedback.
-
刪除結點後,是不是把 P.next 設為 None比較好呢? |
Beta Was this translation helpful? Give feedback.
-
Fig. 在链表中插入与删除结点示意图中,插入操作示例 节点P的插入顺序错了,应该是 |
Beta Was this translation helpful? Give feedback.
-
想问一下github里,主函数里面为什么没有链表的定义,只有链表初始化?谢谢 |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
k神请教一个问题,python里的链表的定义比较好理解,定义了一个ListNode类,这个类有两个属性,一个是存储的值,另一个是指向下一个数据的地址。
但是c++的这个结构体的定义看着我有点迷糊。
|
Beta Was this translation helpful? Give feedback.
-
好清晰的说明,要是当时学数构有这边书就好了。(•̀ω•́ 」∠) |
Beta Was this translation helpful? Give feedback.
-
经典,膜拜的第n遍,每次阅读都有不一样的收获,对于我这种似懂非懂算法的小白来说。非常nice的开源项目~ |
Beta Was this translation helpful? Give feedback.
-
看这个之前自己也买了本书,最终发现还是作者讲得好啊。让我从原理明白了数据结构的魅力 |
Beta Was this translation helpful? Give feedback.
-
可以添加一些链表的常见应用 单向链表单向链表是一种常见的链表结构,每个节点包含一个指向下一个节点的指针。
环形链表首尾相接的链表结构,
双向链表双向链表是一种链表结构,每个节点都包含指向前一个节点和后一个节点的指针。
|
Beta Was this translation helpful? Give feedback.
-
大佬,请教一下,删除链表元素P的时候,N0指向N1就可以了,但是P还是指向的N1,那P最终是被GC回收了吗 |
Beta Was this translation helpful? Give feedback.
-
Java 实现单向链表1、ListNode 定义: public class ListNode<T> {
public T value;
public ListNode<T> next;
public ListNode(T value) {
this.value = value;
}
public ListNode(T value, ListNode<T> next) {
this.value = value;
this.next = next;
}
} 2、实现链表 public class MyLinkedList {
private ListNode<Integer> head = null;
public MyLinkedList() {
}
/**
* 获取指定位置的元素
*
* @param index
* @return
*/
public int get(int index) {
ListNode<Integer> cur = head;
for (int i = 0; i < index; i++) {
if (cur != null) {
cur = cur.next;
}
}
if (cur == null) return -1;
return cur.value;
}
/**
* 头插
*
* @param val
*/
public void addAtHead(int val) {
if (head == null) {
head = new ListNode<>(val);
return;
}
ListNode<Integer> newHead = new ListNode<>(val);
newHead.next = head;
head = newHead;
}
/**
* 尾加
* @param val
*/
public void addAtTail(int val) {
ListNode<Integer> cur = head;
while (cur != null && cur.next != null) {
cur = cur.next;
}
if (cur == null) {
head = new ListNode<>(val);
} else {
cur.next = new ListNode<>(val);
}
}
/**
* 指定位置添加
* @param index
* @param val
*/
public void addAtIndex(int index, int val) {
if (index == 0) {
addAtHead(val);
return;
};
ListNode<Integer> cur = head;
while (--index > 0) {
if (cur != null) {
cur = cur.next;
}
}
if (cur != null) {
ListNode<Integer> node = new ListNode<>(val);
ListNode<Integer> temp = cur.next;
cur.next = node;
node.next = temp;
}
}
/**
* 指定索引删除
*
* @param index
*/
public void deleteAtIndex(int index) {
ListNode<Integer> pre = new ListNode<>(null);
ListNode<Integer> cur = head;
pre.next = head;
while (index-- > 0 && cur != null) {
cur = cur.next;
pre = pre.next;
}
if (pre != null && cur != null) {
pre.next = cur.next;
if (cur == head) {
head = pre.next;
}
}
}
public String toString() {
List<String> list = new ArrayList<>();
ListNode<Integer> cur = head;
while (cur != null) {
list.add(cur.value.toString());
cur = cur.next;
}
return String.join("->", list);
}
} |
Beta Was this translation helpful? Give feedback.
-
C#备忘namespace _1.Console;
public class CustomLinkedList<T>
{
private Node<T>? _head;
public CustomLinkedList(Node<T>? head = null)
{
_head = head;
}
public void Insert(Node<T> node, Node<T> newNode)
{
if (_head == null)
throw new InvalidOperationException("链表为空,无法插入新节点");
if (node == null)
throw new ArgumentNullException(nameof(node));
if (newNode == null)
throw new ArgumentNullException(nameof(newNode));
var temp = node.Next;
node.Next = newNode;
newNode.Next = temp;
}
public void Remove(Node<T> node)
{
if (_head == null)
throw new InvalidOperationException("链表为空");
if (node == null)
throw new ArgumentNullException(nameof(node));
if (node.Next == null)
throw new InvalidOperationException("无法删除最后一个节点");
node.Next = node.Next.Next;
}
public Node<T>? Get(int index)
{
if(_head == null)
throw new InvalidOperationException("链表为空");
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index), "索引不能小于0");
var current = _head;
var i = 0;
while (current.Next != null)
{
if(i == index)
return current;
i++;
current = current.Next;
}
return null;
}
public int FindIndex(T value)
{
if (_head == null)
throw new InvalidOperationException("链表为空");
if (value == null)
throw new ArgumentNullException(nameof(value));
if (_head.Value!.Equals(value)) return 0;
var current = _head;
var i = 0;
while (current.Next != null)
{
if (current.Value!.Equals(value))
return i;
current = current.Next;
i++;
}
return -1;
}
} |
Beta Was this translation helpful? Give feedback.
-
其实可以写简单点 TS 这种可以定义类似结构体的自定义类型的语言可以直接用type写 // 声明链表类型
type ChainNode = {
data: number,
next: ChainNode | null,
prev: ChainNode | null,
};
// 创建头节点
const node_0: ChainNode = {
data: 1,
next: null,
prev: null,
};
// 创建第二个节点
const node_1: ChainNode = {
data: 2,
next: null,
prev: node_0,
};
// 修改头节点属性 链接两个节点
node_0.next = node_1; |
Beta Was this translation helpful? Give feedback.
-
打卡6月11日 |
Beta Was this translation helpful? Give feedback.
-
感谢来信,你的邮件已经收到。
|
Beta Was this translation helpful? Give feedback.
-
感觉在链表常用操作当中的插入与删除,规定为O(N)比较好吧,虽然几个指针的指向改变是O(1)级,但是考虑最差情况,遍历链表就需要O(N)的时间,两者取更大的情况记为O(N)更合理一些. |
Beta Was this translation helpful? Give feedback.
-
python语法小白笔记
self.next: ListNode | None = None
if __name__ == "__main__":
```只在执行文件时运行,作为模块导入时需要放在`if __name__ == "__main__":`之外
3.函数参数
```python
def print_list(head: ListNode):
while head:
print(head.val, end=" -> " if head.next else "\n")
head = head.next
|
Beta Was this translation helpful? Give feedback.
-
c才是好玩哇,指针,指针的地址 typedef int ElemType; typedef struct LinkNode { // 创建新节点 // 初始化链表(头节点) // 头插法创建链表 // 尾插法创建链表
} // 打印链表
} // 检查链表是否为空 // 释放链表内存 int main() {
} |
Beta Was this translation helpful? Give feedback.
-
感谢来信,你的邮件已经收到。
|
Beta Was this translation helpful? Give feedback.
-
把我的也分享一下,Rust
|
Beta Was this translation helpful? Give feedback.
-
感谢来信,你的邮件已经收到。
|
Beta Was this translation helpful? Give feedback.
-
在java中由于不考虑内存清理问题,所以,链表节点的删除,也可以直接如下: |
Beta Was this translation helpful? Give feedback.
-
感谢来信,你的邮件已经收到。
|
Beta Was this translation helpful? Give feedback.
-
抱歉我也不太清楚,我之前在网页上留过言,但是之后不知道为什么一直会抄送,如果打扰到你真的抱歉
zhao
***@***.***
|
Beta Was this translation helpful? Give feedback.
-
我不知道,好像是平台的设置,我个人没有抄送
…-----原始邮件-----
发件人:HNUIsak ***@***.***>
发送时间:2025-09-25 17:34:09 (星期四)
收件人: krahets/hello-algo ***@***.***>
抄送: kang_wang ***@***.***>, Comment ***@***.***>
主题: Re: [krahets/hello-algo] chapter_array_and_linkedlist/linked_list/ (Discussion #83)
抱歉不好意思打扰一下 请问一下为什么我一直能收到来自您的抄送邮件呢?
在 2025-09-24 17:18:53,"iLoveRatRace" ***@***.***> 写道:
感谢来信,你的邮件已经收到。
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
Kang Wang
Haiyun campus, Xiamen University, Siming District, Xiamen 361005
Department of Mathematical Sciences · Department of Information and Computational Mathematics · Laboratory Building 501
Phone: +86 184 5554 3368
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
chapter_array_and_linkedlist/linked_list/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/
Beta Was this translation helpful? Give feedback.
All reactions