首页 > 其他 > 详细

19. Remove Nth Node From End of List

时间:2019-12-29 16:35:45      阅读:71      评论:0      收藏:0      [点我收藏+]
  • 第一种解法
  /**
   * Definition for singly-linked list.
   * struct ListNode {
   *     int val;
   *     ListNode *next;
   *     ListNode(int x) : val(x), next(NULL) {}
   * };
   */
  class Solution 
  {
  public:
      ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        int count = 0;
        ListNode* pre = head;  
        if(head == NULL)
        {
            return NULL;
        }
        
          while(pre != NULL)
        {
            count++;
            pre = pre->next;
              
        }
          
        if(count == n)  //删除头节点
        {
            pre = head;
            return pre->next;
        }
        
        pre = head;
          int i = 1;
        while(pre != NULL)
        {
            if(i == count - n)
            {
                break;
            }
            i++;
              pre = pre->next;
        }
        //删除一个节点
        ListNode* temp;
        temp = pre->next;
          
        pre->next = temp->next;
        delete temp;
        
        return head;
      }
  };

链表删除节点关键是找到待删除节点的前一个节点。另外就是注意要删除头节点的情况。在上面的代码中

        if(count == n)  //删除头节点
        {
            pre = head;
            return pre->next;
        }

只是把头节点的下一个元素返回了,并没有真正意义上的释放头节点指向的内存。因此将代码修改如下:

  • 修改后代码
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution 
    {
    public:
        ListNode* removeNthFromEnd(ListNode* head, int n) 
        {
            int count = 0;
            ListNode* pre = head;  
            if(head == NULL)
            {
                return NULL;
            }
            
            while(pre != NULL)
            {
                count++;
                pre = pre->next;
            }
            
            if(count == n)  //删除头节点
            {
                pre = head;
                head = head->next;
                delete pre;
                return head;
            }
            
            pre = head;
             int i = 1;
            while(pre != NULL)
            {
                if(i == count - n)
                {
                    break;
                }
                i++;
                pre = pre->next;
            }
            //删除一个节点
            ListNode* temp;
            temp = pre->next;
            
            pre->next = temp->next;
            delete temp;
            
            return head;
        }
    };
  • 快慢指针法

    声明一个快指针fast和一个慢指针slow。首先fast移动到待删除节点temp之前,然后fastslow每次移动一步,当fast移动到最后时,slow指针正好移动到待删除节点temp之前,这时完成删除操作即可。

  /**
   * Definition for singly-linked list.
   * struct ListNode {
   *     int val;
   *     ListNode *next;
   *     ListNode(int x) : val(x), next(NULL) {}
   * };
   */
  class Solution 
  {
  public:
      ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        if(head == NULL)
        {
            return NULL;
        }
        
        ListNode* newhead = new ListNode(0);
        newhead->next = head;
        
          ListNode* fast = newhead;
        ListNode* slow = newhead;
        
        for(int i = 0;i < n;i++)
        {
            fast = fast->next;  
        }
        
        while(fast->next)
        {
            fast = fast->next;
            slow = slow->next;
        }
        
        ListNode* temp = slow->next;  //temp表示待删除的节点
          slow->next = slow->next->next;
                
        delete temp;
        
        return newhead->next;
      }
  };

19. Remove Nth Node From End of List

原文:https://www.cnblogs.com/Manual-Linux/p/12115217.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!