當(dāng)前位置:
首頁(yè) >
C++ leetcode 19. 删除链表的倒数第N个节点 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
發(fā)布時(shí)間:2025/4/16
43
豆豆
生活随笔
收集整理的這篇文章主要介紹了
C++ leetcode 19. 删除链表的倒数第N个节点 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、思路:
? ? ?遍歷一遍存儲(chǔ)節(jié)點(diǎn)到vector數(shù)組中,然后利用數(shù)組指向倒數(shù)第n個(gè),將倒數(shù)n-1的節(jié)點(diǎn)的next指向倒數(shù)n的next
二、代碼:
class Solution { public:ListNode* removeNthFromEnd(ListNode* head, int n) {vector<ListNode*>nodeLists;ListNode *nowNode = head;while (nowNode != NULL) {nodeLists.push_back(nowNode);nowNode = nowNode->next;}int index = nodeLists.size() - n;if (index <= 0)return head->next;nodeLists[index - 1]->next = nodeLists[index]->next;delete nodeLists[index];return head;} };?
總結(jié)
以上是生活随笔為你收集整理的C++ leetcode 19. 删除链表的倒数第N个节点 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: leetCode C++ 49. 字母异
- 下一篇: leetcode C++ 链表 24.