Reversing a linked list

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

I remember when I studied data structure. Your course help me a lot to understand the core logic. It's always seems not easy in C or C++ language especially for beginner but learn it is really useful. For instance to understand how high level languages like Java, c#, etc. work with that. It's a very important topic. And thank you to explain it in a really great way. I always recommend your channel for my student and for everyone who want to learn C even if they don't speak English like me :) . Keep going!

jameslaurino
Автор

Thank you, you’re a marvelous teacher. 👍🏻💻📚

nto
Автор

Your videos are amazing. You're a great teacher. Keep it up!

Woshii
Автор

At first, thank you for this videos, are very useful.
But I think in your code it's important to update tail and head if is the case, especially when the list is completely empty, I show you my code (i'm using structs, because is part of a project)

void remove_node_a(t_content *node, t_push *data)
{
if (node->prev != NULL)
node->prev->next = node->next;
else
data->a_tail = node->next;
if (node->next != NULL)
node->next->prev = node->prev;
else
data->a_head = node->prev;
free(node);
}

Here the declaration of the structs to clarify the code (but i only add the 2 else)

typedef struct s_content
{

struct s_content *next;
struct s_content *prev;
} t_content;

typedef struct s_push
{
char **input;
t_content *a_tail;
t_content *a_head;
t_content *b_tail;
t_content *b_head;
} t_push;

zikocult
Автор

I think it would be easier to understand the logic oh you're switching the nodes around because I got so confused when you explained it I had to run it in the deguber to understand what happened. Basically current, previous, and next they are like sublist after they go inside the function this why you were able to switch them around.

Tryandreview
Автор

Thank you for the explanation. My question is what if the linked list is more than 3 elements long?

tarekhamdoudi
Автор

9:30 my recursive solution:
```
Node *reverse(Node **node, Node *prev) {
if(*node == NULL) return prev;
Node *next = (*node)->next;
(*node)->next = prev;
return *node = reverse(&next, *node);
}
```

Just call it like that: reverse(&root, NULL);

H-pv
Автор

Here is my recursive version. I'm proud with this.

Node* reverse(Node** node, Node* prev){
Node* last_node = NULL;
if((*node) -> next != NULL){
last_node = reverse(&((*node) -> next), *node);
}
(*node) -> next = prev;
if(last_node == NULL){
last_node = *node;
}
*node = last_node;
return last_node;
}

Call it like: reverse(&root, NULL);

nandorboda