leetcode 19 删除链表的倒数第n个节点

简介: leetcode 19 删除链表的倒数第n个节点

删除链表的倒数第n个节点


11741588d8884e1385d73080a674ef38.png

遍历算出数目,再删除

#include <iostream>
#include <vector>
#include<algorithm> 
using namespace std;
 struct ListNode {
   int val;
   ListNode *next;
   ListNode() : val(0), next(nullptr) {}
   ListNode(int x) : val(x), next(nullptr) {}
   ListNode(int x, ListNode *next) : val(x), next(next) {}
 };
  class Solution {
  public:
      ListNode* removeNthFromEnd(ListNode* head, int n) {
          int length = 0;
          ListNode* temp1 = head , *temp2;
          while (temp1!=nullptr)
          {
              temp1 = temp1->next;
              length++;
          }
          if (n == length)
          {
              temp2 = head;
              head = head->next;
              delete temp2;
          }
          else
          {
              temp1 = head;
              for (int i = 0; i < length - n - 1; i++ )
              {
                  temp1 = temp1->next;
              }
              temp1->next = temp1->next->next;
          }
          return head;
      }
  };
int main()
{
    vector<int> head = { 1,2 ,3,4 ,5};
    ListNode* head_test = new ListNode(0);
    ListNode* test  , *cur = head_test;
    Solution  a;
    for (int i = 0; i < head.size(); i++)
    {
       ListNode* temp = new ListNode(head[i]);
       cur->next = temp;
       cur = cur->next;
    }
    cur->next = nullptr;
    cur = head_test;
    cout << "cur list" << endl;
    while (cur->next != nullptr)
    {
        cout << cur->val << ' ';
        cur = cur->next;
    }
    cout << cur->val << endl;
    test = a.removeNthFromEnd(head_test->next,2);
    while (test->next != nullptr)
    {
        cout << test->val << ' ';
        test = test->next;
    }
    cout << test->val << ' ';
  return 0;
}

双指针

快慢双指针,两个指针直接相隔n个

  class Solution {
  public:
      ListNode* removeNthFromEnd(ListNode* head, int n) {
          ListNode  *pre ,*aft ,*temp;
          ListNode* dunmy_head = new ListNode(0, nullptr);
          dunmy_head->next = head;
          pre = dunmy_head;
          aft = pre;
          for (int i = 0; i < n+1; i++)
          {
              aft = aft->next;
          }
          while (aft != nullptr)
          {
              pre = pre->next;
              aft = aft->next; 
          }
          cout << pre->val << endl;
          temp = pre->next;
          pre->next = pre->next->next;
          delete temp;
          return dunmy_head->next;
      }
  };

双指针法 ,卡尔

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* slow = dummyHead;
        ListNode* fast = dummyHead;
        while(n-- && fast != NULL) {
            fast = fast->next;
        }
        fast = fast->next; // fast再提前走一步,因为需要让slow指向删除节点的上一个节点
        while (fast != NULL) {
            fast = fast->next;
            slow = slow->next;
        }
        slow->next = slow->next->next;
        return dummyHead->next;
    }
};


二刷

遍历数目再删除

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head->next == nullptr && n==1) return nullptr;
        int num = 0;
        ListNode *cur = head;
        while(cur!=nullptr)
        {
            cur = cur->next;
            num++;
        }
        if(n == num ) return head->next;
        else
        {
            num = num - n - 1;
            cur = head;
            while(num--)
                cur = cur->next;
            ListNode *tmp = cur->next->next;
            cur->next = tmp;
        }
        return head;
    }
};

相关文章
|
5月前
【力扣】-- 移除链表元素
【力扣】-- 移除链表元素
56 1
|
5月前
Leetcode第21题(合并两个有序链表)
这篇文章介绍了如何使用非递归和递归方法解决LeetCode第21题,即合并两个有序链表的问题。
78 0
Leetcode第21题(合并两个有序链表)
|
5月前
|
算法
【❤️算法笔记❤️】-每日一刷-19、删除链表的倒数第 N个结点
【❤️算法笔记❤️】-每日一刷-19、删除链表的倒数第 N个结点
105 1
|
5月前
LeetCode第二十四题(两两交换链表中的节点)
这篇文章介绍了LeetCode第24题的解法,即如何通过使用三个指针(preNode, curNode, curNextNode)来两两交换链表中的节点,并提供了详细的代码实现。
52 0
LeetCode第二十四题(两两交换链表中的节点)
|
5月前
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
71 0
|
5月前
【LeetCode 46】450.删除二叉搜索树的节点
【LeetCode 46】450.删除二叉搜索树的节点
40 0
|
5月前
|
索引
力扣(LeetCode)数据结构练习题(3)------链表
力扣(LeetCode)数据结构练习题(3)------链表
132 0
|
9月前
|
存储 SQL 算法
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
|
9月前
|
存储 SQL 算法
LeetCode 题目 86:分隔链表
LeetCode 题目 86:分隔链表
|
9月前
|
存储 算法 Java
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
79 2