1 /**2 * id:193 * Name:remove Nth Node from End of List4 * Data structure:linked List5 * Time Complexity:6 * Space Complexity:7 * Tag:linklist8 * Difficult:easy9 Ten * Problem: One * Given A linked list, remove the nth node from the end of the list and return its head. A * 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. - * Try to do this in one pass the - * Solution: - * 1 -- - * Just run one pass to get the length of the list and then caculate the position from the beginning. + * then delete that item. - * 2 -- + * Use one vector to store the every Node and then we can use the "index in the" array to operate the link A * List directly. at - * What to learn: - * When to consider give some cur->next value, firstly you should consider the original value maybe NULL. - - * Definition for singly-linked list. - * struct ListNode { in * int val; - * ListNode *next; to * ListNode (int x): Val (x), Next (NULL) {} + * }; - **/ the *#include <iostream> $#include <vector>Panax Notoginseng using namespacestd; - the + structListNode { A intVal; theListNode *Next; +ListNode (intx): Val (x), Next (NULL) {} - }; $ $ voidPrint (ListNode *l) - { -ListNode *head =l; the while(head!=NULL) - {Wuyicout<< head->val<<Endl; theHead = head->Next; - } Wu } - About /* $ class Solution { - Public : - ListNode *removenthfromend (listnode *head, int n) { - A ListNode *fakenode = new ListNode (0); + fakenode->next = head; the int length = 0; - ListNode * p = fakenode; $ While (p) the { the length++; the p = p->next; the } - length--; in delete p; the int cnt = length-n; the ListNode *cur = Fakenode; About While (CNT) the { the cur=cur->next; the CNT--; + } - if (cur->next) the Cur->next = cur->next->next;Bayi return fakenode->next; the } the }; - */ - the classSolution { the Public: theListNode *removenthfromend (ListNode *head,intN) { the if(Head = =NULL) - returnNULL; the theStd::vector<listnode *>v; theListNode *p =head;94 while(P) the { the V.push_back (p); thep = p->Next;98 } About //cout<<v.size (); - intPosition = V.size ()-N;101 if(Position = =0)102 returnV[position]->Next;103 Else if(Position = = V.size ()-1)104v[position-1]->next =NULL; the Else106v[position-1]->next = v[position]->Next;107 returnv[0];108 109 } the };111 thelistnode* Create_list (int* Array,intN)113 { the if(n==0) the returnNULL; theListNode *head,*Current ;117Head = current =NewListNode (array[0]);118 for(intI=1; i<n;i++) 119 { -ListNode *node =NewListNode (Array[i]);121Current-and Next =node;122Current = currentNext;123 }124 returnhead; the }126 127 - intMain ()129 { the //vector <int> a = {1,3,5};131 solution ans; the //int a[3] = {1,3,5};133 intb[1] = {1};134ListNode * first = Create_list (b,1);135 //ListNode * second = create_list (a,3);136ListNode * Pure = Ans.removenthfromend (First,1);137 if(Pure = =NULL)138printf"NULL");139 Else $ print (pure);141 return 0;142}
Requirements: If you want to use a traversal, then open a new vector, which will save each node's address, easy to operate.
Leetcode Remove Nth Node from End of List