1. 程式人生 > >19. Remove Nth Node From End of List(移除連結串列的倒數第n個節點)

19. Remove Nth Node From End of List(移除連結串列的倒數第n個節點)

問題描述
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.

題目分析
這道題目很明朗,要我們移除連結串列倒數第n個節點,並且已經限定n一定是有效的,即n不會大於連結串列中的元素總數。傳統思路很簡單,可以通過兩次遍歷,第一次求出連結串列的長度,第二次刪除要刪除的節點。但是題目要求我們一次遍歷解決問題,那麼就得想些比較巧妙的方法了。我們必須通過一次遍歷找到倒數第N個節點,那麼我們需要用兩個指標來幫助我們解題p和q。首先q指標先向前走n步,與p指標保持n個節點的距離,然後p指標和q指標同時向連結串列尾移動,直到q為最後一個元素時停止,此時p指向要移除元素的前一個元素,我們只需要刪除p的下一個節點即可。

程式碼展示

#include <iostream>
#include <stdlib.h> #include <string> using namespace std; struct ListNode { //定義連結串列結構 int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { if (head->next==NULL) return
NULL; //判斷連結串列是否只有1個節點 ListNode *p = head, *q = head; //定義兩個指標 while(n--) q = q->next; //使p指標與q指標相距n個節點 if (q == NULL) return head->next; while(q->next) { //p指標與q指標同時向連結串列末尾遍歷,直至q指標指向連結串列尾 q = q->next; p = p->next; } p->next = p->next->next; //此時p指標指向要刪除節點的上一個節點,刪除p節點的下一個節點即可 return head; } }; int main(){ cout<<"輸入連結串列的長度:"; int n; cin>>n; ListNode* head=NULL; ListNode* p; int a; for(int i=0;i<n;i++){ cin>>a; if (head == NULL){ head = (ListNode *)malloc(sizeof(ListNode)); head->val = a; p = head; } else{ p->next = (ListNode *)malloc(sizeof(ListNode)); p = p->next; p->val = a; } } p->next=NULL; Solution solution; cout<<"輸入你想要刪除的倒數節點序號:"; int b; cin>>b; ListNode* result = solution.removeNthFromEnd(head,b); while(result!=NULL){ cout<<result->val<<" "; result=result->next; } cout<<endl; return 0; }

執行結果展示
這裡寫圖片描述
這裡寫圖片描述

相關推薦

19. Remove Nth Node From End of List連結串列倒數n節點

問題描述 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->

LeetCode | Remove Nth Node From End of List連結串列倒數n結點

題目: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->

19.Remove Nth Node From End of List單鏈表中倒數N結點

Given a linked list, remove the nth node from the end of list and return its head. For example,

【LeetCode-面試演算法經典-Java實現】【019-Remove Nth Node From End of List單鏈表的倒數N節點

原題   Given a linked list, remove the nth node from the end of list and return its head.   F

【LeetCode】19. Remove Nth Node From End of ListC++

地址:https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 題目: Given a linked list, remove the

19. Remove Nth Node From End of List連結串列

https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 題目:刪除倒數第n個節點(遍歷只能一趟) 思路:雙指標 例如:1->2->3->4->5, n =

LeetCode 19. Remove Nth Node From End of List刪除單鏈表倒數N結點

題目描述:    Given a linked list, remove the nth node from the end of list and return its head.例子: Give

19. Remove Nth Node From End of List 雙指標

Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n =

[LeetCode] Remove Nth Node From End of List 連結串列倒數N節點

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

19. Remove Nth Node From End of List

while linked front cnblogs from ++ ava style solution https://leetcode.com/problemset/all/?search=19 涉及鏈表刪除操作的時候,穩妥起見都用 dummynode,省去很多麻煩。

leetcode 19. Remove Nth Node From End of List

刪除 else logs tco nth -1 move col n-1 Given a linked list, remove the nth node from the end of list and return its head. For example, G

19. Remove Nth Node From End of List 刪除倒數n節點

得到 listnode 負責 style pre def 正數 always ast Given a linked list, remove the nth node from the end of list and return its head. For exampl

【leetcode】19. Remove Nth Node From End of List

log become 兩個 str note count con not one Given a linked list, remove the nth node from the end of list and return its head. For example

leetcode#19 Remove Nth Node From End of List

ber 指針 lang bsp for style Language 示例 opera 給定一個鏈表,刪除鏈表的倒數第 n 個節點,並且返回鏈表的頭結點。 示例: 給定一個鏈表: 1->2->3->4->5, 和 n = 2. 當刪除了倒數第二個節

19-remove-nth-node-from-end-of-list

題目描述: Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4-&

【Leetcode】【連結串列19. Remove Nth Node From End of List / 刪除連結串列倒數N節點

Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and

[leetcode] 19. Remove Nth Node From End of List (Medium)

原題連結 刪除單向連結串列的倒數第n個結點。 思路: 用兩個索引一前一後,同時遍歷,當後一個索引值為null時,此時前一個索引表示的節點即為要刪除的節點。 Runtime: 13 ms, faster than 24.49% of Java class Solution { public Lis

【LeetCode】19. Remove Nth Node From End of List - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given a linked list, remove the n-th node from the end of list and retur

19 Remove Nth Node From End of List

別想花裡胡哨的了。。倒置刪除再倒置吧,複雜度也就n,就是程式碼長了點 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *

19. Remove Nth Node From End of List C++

使用雙指標法,可以僅遍歷一次完成節點的定位 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : va