美文网首页
链表与数组的删除问题

链表与数组的删除问题

作者: juexin | 来源:发表于2017-04-18 16:18 被阅读0次

**83. Remove Duplicates from Sorted List **
Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
代码如下:

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        ListNode* newhead = new ListNode(-1);
        newhead->next = head;
        if(head==NULL)
           return NULL;
        ListNode* p1 = head;
        ListNode* p2 = head->next;
        while(p1!=NULL&&p2!=NULL)
        {
           if(p1->val!=p2->val)
           {
               p1 = p1->next;
               p2 = p2->next;
           }
           else
           {
               p1->next = p2->next;
               p2 = p2->next;
           }
        }
        return newhead->next;
    }
};

**82. Remove Duplicates from Sorted List II **
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
代码如下:

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        if(head==NULL)
           return NULL;
        ListNode* newhead = new ListNode(-1);
        newhead->next = head;
        ListNode* p1 = head->next;
        ListNode* p2 = head;
        ListNode* p3 = newhead;
        
        while(p1&&p2)
        {
          if(p1->val!=p2->val)  
          {
              p1 = p1->next;
              p2 = p2->next;
              p3 = p3->next;
          }
          else
          {
              while(p1!=NULL&&p1->val==p2->val)
              {
                  p1 = p1->next;
              }
              
              p2 = p1;
              p3->next = p1;
              p1 = p1->next;
          }
        }
        return newhead->next;
    }
};

**19. Remove Nth Node From End of List **
Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.
代码如下:

class Solution {
public:
    ListNode *removeNthFromEnd(ListNode *head, int n) {
        int k = getLength(head);
        if(n>k)
          return NULL;
        ListNode* newhead = new ListNode(-1);
        newhead->next = head;
        ListNode* fast = newhead;

        int t = k - n;
        while(t>0)
        {
           fast = fast->next;
           t--;
        }
        fast->next = fast->next->next; 
        return newhead->next;
        
    }
    int getLength(ListNode* list)
    {
        int count = 0;
        while(list!=NULL)
        {
           list = list->next;
           count++;
        }
        return count;
    }
};

**26. Remove Duplicates from Sorted Array **
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
代码如下:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int n = nums.size();
        if(n<=1)
          return n;
        int j = 1;
        
        for(int i=1;i<n;i++)
        {
            if(nums[i]==nums[j-1])
              continue;
            else
              nums[j++] = nums[i];
        }
        return j;
    }
};

**80. Remove Duplicates from Sorted Array II **
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
代码如下:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int n = nums.size();
        if(n<2)
          return n;
        int j=2;
        for(int i=2;i<n;i++)
        {
            if(nums[i]==nums[j-1]&&nums[i]==nums[j-2])
              continue;
            else
              nums[j++] = nums[i];
        }
        return j;
    }
};

**27. Remove Element **
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3], val = 3
Your function should return length = 2, with the first two elements of nums being 2.
代码如下:

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int n = nums.size();
        int j = 0;
        
        for(int i=0;i<n;i++)
        {
            if(nums[i]!=val)
              nums[j++] = nums[i];
        }
        return j;
    }
};

相关文章

  • 链表与数组的删除问题

    **83. Remove Duplicates from Sorted List **Given a sorted...

  • [AlgoGo]线性存储结构-链表

    与数组的对比 方面数组链表内存空间连续离散插入删除O(n)O(1)随机访问O(1)O(n) 链表插入、删除操作不需...

  • 链表

    链表 缺点:查找复杂有点:定点删除/插入元素 单链表 双向链表 循环链表 双向循环链表 数组与链表的区别 数据存储...

  • 数据结构动画描述

    数组 插入数组插入 删除数组删除 链表 栈 队列 二分搜索树 插入

  • 链表

    链表和数组一样也支持查找,插入,删除操作。最常见的三种链表是:单链表,双链表和循环链表。 链表和数组的区别:数组需...

  • day3 哈希表

    哈希表 是由数组跟链表组合而成的产物特点: 数组(顺序表)寻址容易 链表:插入删除容易 哈希表:寻址容易,插入删除...

  • HashMap、linkedHashMap与treeHashMa

    java底层两种数据结构,数组与链表。数组地址连续,查询速度快,但是增加和删除的速度慢,链表与此相反。hashma...

  • 《算法图解》笔记——选择排序

    数组和链表 数组链表读取O(1)O(n)插入O(n)O(1)删除O(n)O(1) (仅当能够立即访问到要删除的元素...

  • 2018-07-26

    合并有顺序的数组 打印两个有序链表的公共部分 在单链表和双链表中删除倒数第k个节点 单链表 双链表 删除链表的中间...

  • HashMap的一些理解

    HashMap的结构: 数组的寻址快,但是数据的插入与删除速度不行。链表的插入与删除速度快,但是寻址速度不行。那有...

网友评论

      本文标题:链表与数组的删除问题

      本文链接:https://www.haomeiwen.com/subject/arxvittx.html