【问题标题】:Removing elements from singly-linked list (C++)从单链表中删除元素 (C++)
【发布时间】:2017-10-21 23:10:20
【问题描述】:

给定一个由整数 l 和一个整数 k 组成的单链表,如何从列表 l 中删除所有值为 k 的元素?最好在 O(n) 时间内。这是我所拥有的:

// Definition for singly-linked list:
// template<typename T>
// struct ListNode {
//   ListNode(const T &v) : value(v), next(nullptr) {}
//   T value;
//   ListNode *next;
// };
//
ListNode<int> * removeKFromList(ListNode<int> * l, int k) {
ListNode iterator = l;
while(iterator != NULL)
  {
    if(iterator->value == k)
    {

    }
    iterator = iterator->next;
  }
}

有什么建议吗?

【问题讨论】:

  • 您遇到了什么问题?您发布的代码是一个存根:您是否尝试过实现实际的删除机制?
  • 如果您将iterator = iterator-&gt;next; 放在else 中,可能会更容易。如果您只是在if 中删除了iterator 处的节点,则您不希望此运行。
  • 提示 #1:使用 std::liststd::forward_list。提示 #2:查找删除/擦除习语。

标签: c++ algorithm


【解决方案1】:

这个怎么样?

ListNode1<int> * removeKFromList(ListNode1<int> * l, int k) {
          ListNode1<int> *tmp = l,*prev=NULL;
          if (l==NULL) return l;
          while (tmp) {
              if (tmp->value == k)
              {
                  if (prev)
                      prev->next=tmp->next;
                  else
                      l=l->next;
                  //delete tmp; ??
              }
              else
                  prev=tmp;

              tmp=tmp->next;
          }
          return l;
      }

【讨论】:

    【解决方案2】:
    def removeKFromList(l, k):
        if(l is None):
            return l
        curr = l
        final = ListNode(0)
        i = 1
        newhead = l
        while(curr):
            if(curr.value != k and i):
                i = 0
                newhead = curr
            if(curr.value == k):
                final.next = curr.next
                tmp = final.next
                if(tmp is None and i):
                    return None
            else:
                final = curr
            curr = curr.next
        return newhead
    

    【讨论】:

    • 请为您的回答提供一些背景信息。它将帮助访问该站点的其他人轻松理解代码。
    【解决方案3】:

    **这里是我解决问题的方法和每一步的解释

    # Singly-linked lists are already defined with this interface:
    # class ListNode(object):
    #   def __init__(self, x):
    #     self.value = x
    #     self.next = None
    #
    def solution(l, k):
        
        """ 
        - First, check if the is empty or not, so return the empty list itself
        """
        if l is None:
            return l
    
        """
        - Then trying to remove the occurrences of element k in the head of the 
        linked list ( It may appear several consecutive k elements at the 
        beginning of the linked list. Even the whole linked list may compose of
        nodes with k values all )
        """
        while l.value == k and l != None:
            l = l.next
            if l == None:
                break
        """
        - Then I am checking again if the list is empty after the previous cleaning
        operation in the while loop, because it may remove all of the nodes from 
        the list ( this is a special case when the whole linked list may compose of 
        nodes with k values all)
        """
        if l is None:
            return l
    
        """
        - Then I am finally checking all of the remaining nodes in the list 
        and removing the connection of the nodes where the value is equal to k.
        """
        curr = l
        while curr.next:
            if int(curr.next.value == k):
                curr.next = curr.next.next
            else:
                curr = curr.next
            
        return l
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 2020-09-04
      • 2021-03-26
      • 2020-10-29
      • 2018-07-08
      • 2017-11-17
      相关资源
      最近更新 更多