标签:
1 /** 2 * ID: 19 3 * Name: Remove Nth Node From End of List 4 * Data Structure: Linked List 5 * Time Complexity: 6 * Space Complexity: 7 * Tag: LinkList 8 * Difficult: Easy 9 10 * Problem: 11 * Given a linked list, remove the nth node from the end of list and return its head. 12 * Given linked list: 1->2->3->4->5, and n = 2. 13 * After removing the second node from the end, the linked list becomes 1->2->3->5. 14 * Try to do this in one pass 15 16 * Solution: 17 * 1 --> 18 * just run one pass to get the length of the list ,then caculate the position from the beginning. 19 * then delete that item. 20 * 2 --> 21 * Use one vector to store the every Node, then we can use the index in the array to operate the link 22 * list directly. 23 24 * What to learn: 25 * When to consider give some cur->next value, firstly you should consider the original value maybe NULL. 26 27 * Definition for singly-linked list. 28 * struct ListNode { 29 * int val; 30 * ListNode *next; 31 * ListNode(int x) : val(x), next(NULL) {} 32 * }; 33 **/ 34 35 #include <iostream> 36 #include <vector> 37 using namespace std; 38 39 40 struct ListNode { 41 int val; 42 ListNode *next; 43 ListNode(int x) : val(x), next(NULL) {} 44 }; 45 46 void print(ListNode *l) 47 { 48 ListNode *head = l; 49 while(head!=NULL) 50 { 51 cout<< head->val<<endl; 52 head = head-> next; 53 } 54 } 55 56 /* 57 class Solution { 58 public: 59 ListNode *removeNthFromEnd(ListNode *head, int n) { 60 61 ListNode *fakenode = new ListNode(0); 62 fakenode->next = head; 63 int length = 0; 64 ListNode * p = fakenode; 65 while(p) 66 { 67 length++; 68 p = p->next; 69 } 70 length--; 71 delete p; 72 int cnt = length-n; 73 ListNode *cur = fakenode; 74 while(cnt) 75 { 76 cur=cur->next; 77 cnt --; 78 } 79 if(cur->next) 80 cur->next = cur->next->next; 81 return fakenode->next; 82 } 83 }; 84 */ 85 86 class Solution { 87 public: 88 ListNode *removeNthFromEnd(ListNode *head, int n) { 89 if(head == NULL) 90 return NULL; 91 92 std::vector<ListNode *> v; 93 ListNode *p = head; 94 while(p) 95 { 96 v.push_back(p); 97 p = p->next; 98 } 99 //cout<<v.size(); 100 int position = v.size() - n; 101 if(position == 0) 102 return v[position]->next; 103 else if(position == v.size()-1) 104 v[position-1]->next = NULL; 105 else 106 v[position-1]->next = v[position]->next; 107 return v[0]; 108 109 } 110 }; 111 112 ListNode* create_list(int* array,int n) 113 { 114 if (n==0) 115 return NULL; 116 ListNode *head,*current; 117 head = current = new ListNode(array[0]); 118 for (int i=1;i<n;i++) 119 { 120 ListNode *node = new ListNode(array[i]); 121 current -> next = node; 122 current = current -> next; 123 } 124 return head; 125 } 126 127 128 int main() 129 { 130 //vector <int> a = {1,3,5}; 131 Solution ans; 132 //int a[3] = {1,3,5}; 133 int b[1] = {1}; 134 ListNode * first = create_list(b,1); 135 //ListNode * second = create_list(a,3); 136 ListNode * pure = ans.removeNthFromEnd(first,1); 137 if(pure == NULL) 138 printf("NULL"); 139 else 140 print(pure); 141 return 0; 142 }
要求: 如果要使用 一次遍历,那么就新开一个vector,里面存上每个node的地址,便于操作。
Leetcode 19 Remove Nth Node From End of List
标签:
原文地址:http://www.cnblogs.com/zhuguanyu33/p/4417714.html