【问题标题】:Delete all the last node of linked list which have value = 0删除链表最后一个值为 0 的节点
【发布时间】:2015-11-01 23:18:01
【问题描述】:

我有一个这样的链接列表:1 0 1 2 0 0 0。 我想删除所有最后一个“0”节点,所以我的列表看起来像:1 0 1 2。 我尝试使用递归:

Node *trimtList(Node* head) {
    if (head->next->data == 0 && head->next->next == NULL) {
        head->next = NULL;
        return head;
    }
    trimList(head->next);
    return head;
}

我意识到这个方法只是删除最后一个0,而不是所有最后一个0...

Node *trimtList(Node* head) {
    if (head && !trimtList(head->next) && head->data == 0) {
        delete head;
        head = nullptr;
    }
    return head;
}

int main() {
    List a;
    a.head= new Node(1);
    a.head->next = new Node(0);
    a.head->next->next = new Node(2);
    a.head->next->next->next = new Node(0);
    a.head->next->next->next->next = new Node(0);
    a.head= trimtList(a.head);
    cout << a << endl;
}

输出是1 0 2 2,然后windows已经停止工作...

【问题讨论】:

    标签: c++ linked-list


    【解决方案1】:

    应该是这样的:

    Node *trimList(Node* head) {
        if (trimList(head->next).next == NULL && head->next->data == 0 ) {
            head->next = NULL;
        }
    
        return head;
    }
    

    通过在开头而不是结尾调用 trimList,我们确保在我们之前最多有一个 0 节点。

    【讨论】:

      【解决方案2】:

      这是我的五分钱。:)

      #include <iostream>
      
      struct Node
      {
          int data;
          Node *next;
      };
      
      void push_front( Node * &head, int data )
      {
          Node *tmp = new Node { data, head };
          head = tmp;
      }
      
      std::ostream & display( Node * &head, std::ostream &os = std::cout )
      {
          for ( Node *tmp = head; tmp; tmp = tmp->next )
          {
              os << tmp->data << ' ';
          }
      
          return os;
      }    
      
      Node * rtrim( Node * &head, int data = 0 )
      {
          if ( head && !rtrim( head->next, data ) && head->data == data )
          {
              delete head;
              head = nullptr;
          }
      
          return head;
      }
      
      int main()
      {
          Node *head = nullptr;
      
          for ( int x : { 0, 0, 0, 2, 1, 0, 1 } ) push_front( head, x );
          display( head ) << std::endl;
      
          rtrim( head );
          display( head ) << std::endl;
      
          rtrim( head, 2 );
          display( head ) << std::endl;
      
          rtrim( head, 1 );
          display( head ) << std::endl;
      
          rtrim( head );
          display( head ) << std::endl;
      
          rtrim( head, 1 );
          display( head ) << std::endl;
      }    
      

      程序输出是

      1 0 1 2 0 0 0 
      1 0 1 2 
      1 0 1 
      1 0 
      1 
      

      简而言之,你自己的函数可能看起来像

      Node *trimtList(Node* head) {
          if ( head && !trimList( head->next ) && head->data == 0 ) {
              delete head;
              head = nullptr;
          }
      
          return head;
      }
      

      应该这样称呼

      head = trimtList( head );
      

      【讨论】:

        猜你喜欢
        • 2016-06-11
        • 2013-03-25
        • 2014-05-22
        • 1970-01-01
        • 2019-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多