链表与数组的删除问题

**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;
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容