chapter_array_and_linkedlist/linked_list/ #83
Replies: 110 comments 196 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.
-
def insert(n0: ListNode, P: ListNode): |
Beta Was this translation helpful? Give feedback.
-
/* 链表节点结构体 */ |
Beta Was this translation helpful? Give feedback.
-
来信获悉,感谢您的支持!耑此奉复 钟冠彬This is an automatic reply, confirming that your e-mail was received.Thank you
|
Beta Was this translation helpful? Give feedback.
-
感谢来信,你的邮件已经收到。
|
Beta Was this translation helpful? Give feedback.
-
我有一个问题:就是这两段代码都能实现相同的功能,而且时间复杂度都是O(n),假如考试的时候刚好考到这个算法,那这两段代码谁得的分最高?还是说都是满分? |
Beta Was this translation helpful? Give feedback.
-
class ListNode: |
Beta Was this translation helpful? Give feedback.
-
我在做题的时候遇到了一个问题:为什么同一个测试用例,在运行的时候能通过,而提交的时候却显示解答错误?感觉不像是时间/空间方面的限制。(题目是leetcode的https://leetcode.cn/problems/copy-list-with-random-pointer/?envType=study-plan-v2&envId=selected-coding-interview)
|
Beta Was this translation helpful? Give feedback.
-
来信获悉,感谢您的支持!耑此奉复 钟冠彬This is an automatic reply, confirming that your e-mail was received.Thank you
|
Beta Was this translation helpful? Give feedback.
-
感谢来信,你的邮件已经收到。
|
Beta Was this translation helpful? Give feedback.
-
时间有限, 简单写一个。。。。。。package com.rui; import java.util.LinkedList; //自己实现一个单向链表 含头节点
} |
Beta Was this translation helpful? Give feedback.
-
C#的代码能不能写.net framework的版本,初学者学不明白那么高级的东西 |
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.
-
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