从单链接列表的开始和结尾删除nth节点

发布于 2025-02-01 08:38:01 字数 1751 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带刺的爱情 2025-02-08 08:38:01

假设您在节点3。

helper = this -> next -> next;
delete this -> next;
this -> next = helper;

因此,基本上您需要在删除之前删除该节点之后到达节点,因为这样可以访问它。

要检查是否有任何节点:

if(root == NULL)
{
 /// there are no nodes
}

如果有节点:

traverse = root;
int count = 0;
while(traverse != NULL)
{
   ++count;
   if(count == n)
   { /* you are at the nth node */ }
   traverse = traverse -> next;
}

请注意,如果您删除节点n并且您仍处于节点(N-1),则只需要进行单独的“ chrive of indience”,所以说,删除另一个节点。因此,如果您想删除以前曾经是PTH的节点,那么只需在if语句中进行


///the deletion
++count;

有效的操作,您将获得计数== p,当您到达PTH的节点直到任何删除。

Suppose you are at node 3.

helper = this -> next -> next;
delete this -> next;
this -> next = helper;

So basically you need to get to the node after the one you seek to delete prior to that deletion as then there will be no way of accessing it.

To check if there are any nodes at all:

if(root == NULL)
{
 /// there are no nodes
}

If there are nodes:

traverse = root;
int count = 0;
while(traverse != NULL)
{
   ++count;
   if(count == n)
   { /* you are at the nth node */ }
   traverse = traverse -> next;
}

Notice that if you delete node n and you are still at node (n-1), you will just have to do a seperate "shift of indicies," so to say, to remove another node. So if you want to delete another node that was previously the pth one, then just do in the if statement


///the deletion
++count;

Effectively you will get count == p when you arrive at the node that was the pth one until any deletions.

記憶穿過時間隧道 2025-02-08 08:38:01

对于您和我的初学者来说,这项任务并不简单。

然而,我们,初学者应该互相帮助。

对于C ++中的初学索引,从0开始。

其次,您应该检查从尾部开始的指针是否等于从头部开始的指针。还是一个指针是其他指针指向的节点的下一个数据成员的指针。

例如,如果两个指针彼此平等,则需要删除列表中的一个节点。

我无法编写伪代码。这对我来说太复杂了。
因此,您是

#include <iostream>
#include <iomanip>
#include <functional>

struct ListNode
{
    int val;
    ListNode *next;
};


void clear( ListNode * &head )
{
    while (head)
    {
        delete std::exchange( head, head->next );
    }
}

void create( ListNode *&head, const int a[], size_t n )
{
    clear( head );

    for (ListNode **current = &head; n--; current = &( *current )->next)
    {
        *current = new ListNode{ *a++, nullptr };
    }
}

std::ostream &display( const ListNode *head, std::ostream &os = std::cout )
{
    for (const ListNode *current = head; current != nullptr; current = current->next)
    {
        os << current->val << " -> ";
    }

    return os << "null";
}

void swap( ListNode *¤t )
{
    if (current && current->next)
    {
        ListNode *&next = current->next;
        std::swap( current, next );
        std::swap( current->next, next->next );
        swap( next );
    }
}

bool remove_two_sides_n( ListNode * &head, size_t n )
{
    ListNode **left = &head;

    while (*left && n--) left = &( *left )->next;

    if (*left == nullptr) return false;

    ListNode **right = &head;
    ListNode *last = *left;

    while (last->next)
    {
        right = &( *right )->next;
        last = last->next;
    }


    if (( *right )->next == *left) std::swap( left, right );

    if ( right != left )
    {
        ListNode *tmp = *right;
        *right = ( *right )->next;
        delete tmp;
    }

    ListNode *tmp = *left;
    *left = ( *left )->next;
    delete tmp;

    return true;
}

int main()
{
    const size_t N = 9;

    for (size_t i = 0; i < N + 1; i++)
    {
        ListNode *head = nullptr;
        const int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        create( head, a, sizeof( a ) / sizeof( *a ) );

        std::cout << std::setw( 2 ) << i << ": ";
        display( head ) << '\n';

        remove_two_sides_n( head, i );

        std::cout << std::setw( 2 ) << i << ": ";
        display( head ) << '\n';

        clear( head );

        std::cout << '\n';
    }
}

程序输出是

 0: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 0: 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null

 1: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 1: 1 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9 -> null

 2: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 2: 1 -> 2 -> 4 -> 5 -> 6 -> 8 -> 9 -> null

 3: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 3: 1 -> 2 -> 3 -> 5 -> 7 -> 8 -> 9 -> null

 4: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 4: 1 -> 2 -> 3 -> 4 -> 6 -> 7 -> 8 -> 9 -> null

 5: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 5: 1 -> 2 -> 3 -> 5 -> 7 -> 8 -> 9 -> null

 6: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 6: 1 -> 2 -> 4 -> 5 -> 6 -> 8 -> 9 -> null

 7: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 7: 1 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9 -> null

 8: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 8: 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null

 9: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 9: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null

The task is not simple for such beginners as you and me.

Nevertheless, we, beginners, should help each other.

For starters indices in C++ start from 0.

Secondly you should check whether the pointer starting from the tail is equal to the pointer starting from the head. Or whether one pointer is the pointer of the data member next of the node pointed to by other pointer.

For example if the two pointers are equal each other you need to delete only one node in the list.

I can not write a pseudo code. It is too complicated for me.
So here you are

#include <iostream>
#include <iomanip>
#include <functional>

struct ListNode
{
    int val;
    ListNode *next;
};


void clear( ListNode * &head )
{
    while (head)
    {
        delete std::exchange( head, head->next );
    }
}

void create( ListNode *&head, const int a[], size_t n )
{
    clear( head );

    for (ListNode **current = &head; n--; current = &( *current )->next)
    {
        *current = new ListNode{ *a++, nullptr };
    }
}

std::ostream &display( const ListNode *head, std::ostream &os = std::cout )
{
    for (const ListNode *current = head; current != nullptr; current = current->next)
    {
        os << current->val << " -> ";
    }

    return os << "null";
}

void swap( ListNode *¤t )
{
    if (current && current->next)
    {
        ListNode *&next = current->next;
        std::swap( current, next );
        std::swap( current->next, next->next );
        swap( next );
    }
}

bool remove_two_sides_n( ListNode * &head, size_t n )
{
    ListNode **left = &head;

    while (*left && n--) left = &( *left )->next;

    if (*left == nullptr) return false;

    ListNode **right = &head;
    ListNode *last = *left;

    while (last->next)
    {
        right = &( *right )->next;
        last = last->next;
    }


    if (( *right )->next == *left) std::swap( left, right );

    if ( right != left )
    {
        ListNode *tmp = *right;
        *right = ( *right )->next;
        delete tmp;
    }

    ListNode *tmp = *left;
    *left = ( *left )->next;
    delete tmp;

    return true;
}

int main()
{
    const size_t N = 9;

    for (size_t i = 0; i < N + 1; i++)
    {
        ListNode *head = nullptr;
        const int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        create( head, a, sizeof( a ) / sizeof( *a ) );

        std::cout << std::setw( 2 ) << i << ": ";
        display( head ) << '\n';

        remove_two_sides_n( head, i );

        std::cout << std::setw( 2 ) << i << ": ";
        display( head ) << '\n';

        clear( head );

        std::cout << '\n';
    }
}

The program output is

 0: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 0: 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null

 1: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 1: 1 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9 -> null

 2: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 2: 1 -> 2 -> 4 -> 5 -> 6 -> 8 -> 9 -> null

 3: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 3: 1 -> 2 -> 3 -> 5 -> 7 -> 8 -> 9 -> null

 4: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 4: 1 -> 2 -> 3 -> 4 -> 6 -> 7 -> 8 -> 9 -> null

 5: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 5: 1 -> 2 -> 3 -> 5 -> 7 -> 8 -> 9 -> null

 6: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 6: 1 -> 2 -> 4 -> 5 -> 6 -> 8 -> 9 -> null

 7: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 7: 1 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9 -> null

 8: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 8: 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null

 9: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
 9: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文