【问题标题】:Finding and removing the last occurrence of an element in a (singly) linked list with only one traversal在只有一次遍历的(单个)链表中查找和删除最后一次出现的元素
【发布时间】:2011-09-30 00:19:43
【问题描述】:

是否有可能找到一个元素(例如,一个整数)的最后一次出现并通过列表只进行一次(前向)遍历来删除该节点?

【问题讨论】:

    标签: data-structures singly-linked-list


    【解决方案1】:

    是的。

    每次在遍历中找到要搜索的值时,只需记住上一个条目。遍历完成后,记住的最后一个条目将具有指向要删除的条目的链接,这足以进行删除。

    【讨论】:

      【解决方案2】:
      public void DeleteLastOccurenceOfKey(Node head, int key) 
      {
          Node current=head;
          Node prev=null;
          Node temp=null;
      
          while(current!=null)
          {
              if(current.next!=null && current.next.data==key)
              {
              prev=current;
              temp=current.next;
              }
              current=current.next;
          }
          prev.next=temp.next;
      
      }
      

      DeleteLastOccurenceOfKey(head,25);

      I/P:5 10 15 25 35 25 40 运单:5 10 15 25 35 40

      【讨论】:

        【解决方案3】:
        /*
         * Delete last occurrence of an item from linked list
         * Given a liked list and a key to be deleted. Delete last occurrence of key
         * from linked. The list may have duplicates.
         *
         * Examples:
         *
         * Input:   1->2->3->5->2->10, key = 2`enter code here`
         * Output:  1->2->3->5->10
         */
        
        
        #include <stdio.h>
        #include <stdlib.h>
        #include <assert.h>
        
        typedef struct list_ list;
        struct list_ {
            int d;
            list *next;
        };
        
        
        void insert (list **head, int d) {
            list *tmp = (list *)malloc(sizeof(list));
            assert(tmp);
            tmp->d = d;
            tmp->next = *head;
            *head = tmp;
        }
        
        void printL (list *p) {
            while (p) {
                printf (" %d ", p->d);
                p = p->next;
            }
            printf ("\n");
        }
        
        
        void deletlastOccurence (list **head, int d) {
        
            list *cur = *head;
            list *prev = NULL;
            list *match = NULL;
        
            if (cur == NULL) {
                printf ("list is empty\n");
                return;
            }
        
            /*
             * Special case when there only ONE NODE
             * in the LIST
             */
            if (cur->next == NULL) {
                if (cur->d == d) {
                    printf ("Deleted one node %d\n", cur->d);
                    free(cur);
                    *head = NULL;
                } else {
                    printf(" No match\n");
                }
                return;
            }
        
            /*
             * Keep track of previous node
             */
            while (cur && cur->next) {
        
                if (cur->next->d == d) {
                    prev = cur;
                    match = cur->next;
                }
                cur = cur->next;
            }
        
            if (prev){
                prev->next = match->next;
                printf ("Delete %d\n", match->d);
                free (match);
            } else {
                /*
                 * Special case when the last node is
                 * on the head itself
                 */
                    if ((*head)->d == d) {
                    cur = *head;
                    *head = cur->next;
                    printf("element is at head Delete %d\n", cur->d);
                    free (cur);
                } else {
                    printf ("No match\n");
                }
            }
        
            printL(*head);
        }
        
        
        
        
        int main (int argc , char *argv) {
        
            list *h = NULL;
            insert(&h, 1);
            insert(&h, 2);
            insert(&h, 3);
            insert(&h, 4);
            insert(&h, 5);
            insert(&h, 2);
            insert(&h, 1);
            insert(&h, 6);
            printL(h);
            deletlastOccurence(&h, 6);
            deletlastOccurence(&h, 2);
        }
        

        【讨论】:

          【解决方案4】:
          public void deleteLastOccurence(int value) {
                  Element cur = this.head;
                  Element prev = null;
                  Element tmp = null;
                  
                  if(this.head == null)
                      return;
                  
                  if(this.head.data == value) {
                      this.head = null;
                      return;
                  }
                  
                  while(cur != null) {
                      if(cur.next != null && cur.next.data == value) {
                          prev = cur;
                          tmp = cur.next;
                      }
                      cur = cur.next;
                  }
                  prev.next = tmp.next;
              }
          

          【讨论】:

          • 头部边缘情况错误。仅仅因为该值可能在头部,并不意味着它是该项目的最后一次出现,例如{0,1,1,1,0,0}。此代码只会自动删除第一个节点。
          猜你喜欢
          • 2014-11-12
          • 2021-02-25
          • 2019-10-29
          • 1970-01-01
          • 2017-01-04
          • 2015-11-04
          • 2015-06-29
          • 2022-12-18
          • 1970-01-01
          相关资源
          最近更新 更多