TEST(removeNthFromEnd, caseTest) {
    Solution s;
    
    ListNode n0(0);
    EXPECT_EQ(NULL, s.removeNthFromEnd(&n0, 1));
    
    ListNode n1(1);
    ListNode n2(2);
    n1.next = &n2;
    ListNode* n3 = s.removeNthFromEnd(&n1, 2);
    EXPECT_EQ(&n2, n3);
    EXPECT_EQ(NULL, n3->next);
    
    ListNode n4(4);
    ListNode n5(5);
    ListNode n6(6);
    ListNode n7(7);
    n4.next = &n5;
    n5.next = &n6;
    n6.next = &n7;
    ListNode *n8 = s.removeNthFromEnd(&n4, 4);
    EXPECT_EQ(5, n8->val);
    EXPECT_EQ(6, n8->next->val);
    
}
Example #2
0
int main()
{
	ListNode* test = new ListNode(3);
	test->next = new ListNode(4);
	test->next->next = new ListNode(5);
	test->next->next->next = new ListNode(6);
	test->next->next->next->next = new ListNode(7);
	ListNode* p = test;
	cout << "The linked list is: ";
	while(p)
	{
		cout << p->value << " ";
		p = p->next;
	}

	int n;
	cout << "\nRemove the nth node from end of list, please input n: ";
	cin >> n;

	Solution s;
	ListNode* result = s.removeNthFromEnd(test, n);

	cout << "The result is: ";
	while (result)
	{
		cout << result->value << " ";
		result = result->next;
	}
	cout << endl;
	system("pause");
	return 0;
}
void let_19(){
	ifstream fin;
	fin.open("let_83.txt");
	char tempc;
	int value;
	ListNode* head;
	ListNode*temp=NULL;
	while(fin.peek()!=']'){
		fin>>tempc>>value;
		ListNode* cur=(ListNode*) new ListNode(value);
		if(temp){
			temp->next=cur;
			temp=cur;
		}else{
			temp=cur;
			head=cur;
		}
	}
	Solution sol;
	temp=sol.removeNthFromEnd(head,1);
	while(temp){
		cout<<temp->val<<",";
		temp=temp->next;
	}
	cout<<endl;
}
/*************************在leetcode上直接提交以上代码即可******************************/
int main()
{
	int n = 0;
	ListNode l(-1);
	cout << "请输入链表的整数个数: "<< endl;
	cin >> n;
	cout<<"请输入链表1的所有整数: "<<endl;
	ListNode *prev = &l;
	for(int i = 0; i < n; i++)
	{
		int temp;
		cin >> temp;
		prev->next = new ListNode(temp);
		prev = prev->next;
	}

	ListNode *pl = l.next;
	cout << "请输入需要移除的位置: "<< endl;
	int pos;
	cin >> pos;
	Solution solu;

	ListNode *left = solu.removeNthFromEnd(pl,pos);

	while(left != NULL)
	{
		cout << left->val<< endl;
		left = left->next;		
	}
}
Example #5
0
int main() {
	ListNode* l1 = new ListNode(1); ListNode* l2 = new ListNode(2); ListNode* l3 = new ListNode(3);
	ListNode* l4 = new ListNode(4); ListNode* l5 = new ListNode(5);
	l1->next = l2; l2->next = l3; l3->next = l4; l4->next = l5;
	Solution s;
	ListNode* res = s.removeNthFromEnd(l1, 2);
}
int _tmain(int argc, _TCHAR* argv[])
{
	Solution S;
	ListNode List(1);
	S.removeNthFromEnd(&List, 1);
	return 0;
}
Example #7
0
int main(int argc , char **argv)
{
	Solution sol ;
	//	ListNode e(5 , NULL) ;
	//	ListNode d(4 , &e) ;
	//	ListNode c(3 , &d) ;
	ListNode b(2 , NULL) ;
	ListNode a(1 , &b) ;
	ListNode *p = &a ;
	while(p != NULL)
	{
		std::cout<<p->val <<std::endl ;
		p = p->next ;
	}
	std::cout<<std::endl;
	ListNode *tmp = sol.removeNthFromEnd(&a , 1);
	if(tmp == NULL)
	{
		std::cout<<"no element"<<std::endl; 
	}
	else
	{
		std::cout << tmp->val <<std::endl;

		ListNode *p = &a ;
		while(p != NULL)
		{
			std::cout<<p->val <<std::endl ;
			p = p->next ;
		}
	}
	return 0 ;
}
Example #8
0
int main(int argc, char **argv)
{
  Solution s;
  ListNode *p = new ListNode(1);
  // p -> next = new ListNode(2);
  // p -> next -> next = new ListNode(3);
  ListNode *ret = s.removeNthFromEnd(p, 1);
  cout << ret -> val << endl;
}  
int main ( int argc, char *argv[] ) {
    Solution s = Solution();
    ListNode* head = new ListNode(1);
    addNode(head, 2);
    addNode(head, 3);
    printList(head);
    head = s.removeNthFromEnd(head, 3);
    printList(head);
    return EXIT_SUCCESS;
}
int main() {
  // get 1->2->3->4->5 if n = 54321
  ListNode * root = create_list(54321);
  Solution sol;
  root = sol.removeNthFromEnd(root, 5);
  // print 54321 if list = 1->2->3->4->5
  cout << get_number(root) << endl;
  delete_list(root);
  return 0;
}
int main() {
    Solution solution;
    ListNode* head = new ListNode(1);
    head->next = new ListNode(2);
    head->next->next = new ListNode(3);
    head->next->next->next = new ListNode(4);
    head->next->next->next->next = new ListNode(5);
    ListNode* ret = solution.removeNthFromEnd(head, 2);

    return 0;
}
Example #12
0
int main(){
	Solution obj;
	ListNode *head;
	ListNode node1(1);ListNode node2(6);ListNode node3(5);ListNode node4(5);ListNode node5(1);ListNode node6(1);
	head = &node1;node1.next = &node2;node2.next = &node3;node3.next = &node4;node4.next = &node5;node5.next = &node6;	
	obj.print(head);	
	cout<<obj.getcount(head)<<endl;
	head = obj.removeNthFromEnd(head,7);
	obj.print(head);
	return 0;
}
int main()
{
    Solution s;
    ListNode* node = new ListNode(1);
    node->next = new ListNode(2);
    node->next->next = new ListNode(3);
    node->next->next->next = new ListNode(4);
    node->next->next->next->next = new ListNode(5);
    node = s.removeNthFromEnd(node, 2);
    printList(node);
    return 0;
}
int main(int argc, char const *argv[])
{
  Solution sol;
  int a[] = {1,2,3,4,5};
  int n = sizeof(a) / sizeof(int);
  for (int i = 1; i <= n; ++i) {
    ListNode *head = list_from_array(a, n);
    head = sol.removeNthFromEnd(head, i);
    print_list(head);
  }
  return 0;
}
int main() {
    Solution *s = new Solution();
    ListNode *l1 = new ListNode(1);
    //l1 = insert(l1, 4);
    //l1 = insert(l1, 3);
    //l1 = insert(l1, 2);
    //l1 = insert(l1, 1);
    print(l1);
    ListNode* r = s->removeNthFromEnd(l1, 1);
    print(r);
    return 0;
}
Example #16
0
int main(){
    ListNode* a1 = new ListNode(1);
    ListNode* a2 = new ListNode(2);
	ListNode* a3 = new ListNode(3);
    a1->next = a2;
	a2->next = a3;
    
 
    print(a1);
    
	Solution s;
    print(s.removeNthFromEnd(a1,3));
}
Example #17
0
int main()
{
	ListNode *p1 = new ListNode(1);
	ListNode *p2 = new ListNode(2);

	p1->next = p2;

	Solution slt;
	ListNode *p = slt.removeNthFromEnd(p1, 1);
	while (p != NULL)
	{
		cout << p->val << " ";
		p = p->next;
	}
}
int main(int argc, char **argv) {
    ListNode n5(5, NULL);
    ListNode n4(4, &n5);
    ListNode n3(3, &n4);
    ListNode n2(2, &n3);
    ListNode n1(1, &n2);

    ListNode *head = s.removeNthFromEnd(&n1, 2);
    while (head != NULL) {
        printf("%d ", head->val);
        head = head->next;
    }
    printf("\n");
    return 0;
}
int main()
{
    Solution sol;
    ListNode *p = new ListNode(1);
    ListNode *q = new ListNode(2);
    ListNode *r = new ListNode(3);
    p->next=q;
    q->next=r;
    p=sol.removeNthFromEnd(p,1);
    while(p){
        cout<<p->val<<",";
        p=p->next;
    }
    return 0;
}
int main(){
	ListNode n1(1);
	ListNode n2(2);
	ListNode n3(3);
	n1.next = &n2;
	n2.next = &n3;
	Solution s;
	ListNode* p = s.removeNthFromEnd(&n1, 3);
	
	while (p != NULL){
		cout << p->val << endl;
		p = p->next;
	}
	system("pause");
	return 0;
}
Example #21
0
int main() {
    ListNode A(1);
    ListNode B(2);
    ListNode C(3);
    ListNode D(4);
    ListNode E(5);

    A.next = &B;
    B.next = &C;
    C.next = &D;
    D.next = &E;

    Solution sol;
    ListNode *head = sol.removeNthFromEnd(&A, 2);
    printList(head);
}
int main() {
    int i=0;
    ListNode **a= new ListNode* [10000000];
    ListNode *head,*p;
    Solution question;

    head=creatSimpleList(10);
    printList(head);
    cout<<endl;

    head=question.removeNthFromEnd(head,1);
    printList (head);


    return 0;
}
int main(void) {
    Solution answer;
    ListNode *pHead1 = new ListNode(2);
    ListNode *pNext11 = new ListNode(4);
    pHead1->next = pNext11;
    ListNode *pNext12 = new ListNode(3);
    pNext11->next = pNext12;
    ListNode *p = answer.removeNthFromEnd(pHead1, 2);
    while (p) {
        cout << p->val << " ";
        p = p->next;
    }
    cout << endl;
    system("Pause");
    return 0;
}
Example #24
0
int main()
{
    Solution s;
    ListNode * head = new ListNode(1);
    ListNode * a = new ListNode(2);
    ListNode * b = new ListNode(4);
    head->next = a;
    a->next = b;

    head = s.removeNthFromEnd(head,1);
    while(head != NULL){
        cout<<head->val<<" ";
        head = head->next;
    }
    cout<<endl;
    return 0;
}
int main(int argc, char *argv[]) {
	Solution *s = new Solution;
	ListNode *l1 = new ListNode(1);
	ListNode *l2 = new ListNode(2);
	ListNode *l3 = new ListNode(3);
	ListNode *l4 = new ListNode(4);
	ListNode *l5 = new ListNode(5);
//	l1->next = l2;
//	l2->next = l3;
//	l3->next = l4;
//	l4->next = l5;
	ListNode *tmp = s->removeNthFromEnd(l1, 1);
	while (tmp != NULL) {
		cout << tmp->val << endl;
		tmp = tmp->next;
	}
}
Example #26
0
int main() {
	ListNode *list[5];
	for (int i = 0; i < 5; ++i)
		list[i] = new ListNode(i + 1);
	for (int i = 0; i < 4; ++i)
		list[i] -> next = list[i + 1];
	Solution s;
	list[0] = s.removeNthFromEnd(list[0], 5);
	ListNode *temp = list[0];
	int count = 0;
	while (temp != NULL) {
		if (count <= 10)
			printf("%d->", temp -> val);
		temp = temp -> next;
		count++;
	}
	return 0;
}
Example #27
0
int main() {
  freopen("test.txt", "r", stdin);

  int a[] = {2, 3, 4, 1};
  int len = 4;
  ListNode node(1);
  ListNode* fr = &node;
  for (int i = 0; i < len; ++i) {
    fr->next = new ListNode(a[i]);
    fr = fr->next;
  }
  Solution s;
  node.next = s.removeNthFromEnd(node.next, 4);
  for (ListNode* p = node.next; p; p = p->next)
    cout << p->val << " ";
  cout << endl;
  return 0;
} 
void test()
{
	ListNode *p1 = new ListNode(1);
	ListNode *p2 = new ListNode(2);
	ListNode *p3 = new ListNode(3);
	ListNode *p4 = new ListNode(4);
	ListNode *p5 = new ListNode(5);

	p1->next = p2;
	p2->next = p3;
	p3->next = p4;
	p4->next = p5;
	print(p1);

	Solution s;
	ListNode *head = s.removeNthFromEnd(p1, 2);
	print(head);
}
int main(int argc, const char *argv[]) {
    auto test = [](std::initializer_list<int> li1,
                    int n,
                    std::initializer_list<int> li2) -> bool {
        Solution solution;
        ListNode *list = CreateList(li1);
        ListNode *expected = CreateList(li2);
        ListNode *result = solution.removeNthFromEnd(list, n);
        ShowList(result);
        bool is_equal = EqualsList(result, expected);
        DestroyList(result);
        DestroyList(expected);
        return is_equal;
    };
    
    assert(test({1, 2, 3, 4, 5}, 2, {1, 2, 3, 5}));
    assert(test({1, 2, 3, 4, 5}, 5, {2, 3, 4, 5}));
    
    return 0;
}
Example #30
0
int main(int argc, char const *argv[]) {
	Solution *s = new Solution();
	ListNode *head = new ListNode(1);
	ListNode *node1 = new ListNode(2);
	ListNode *node2 = new ListNode(3);
	ListNode *node3 = new ListNode(4);
	ListNode *node4 = new ListNode(5);
	head->next = node1;
	node1->next = node2;
	node2->next = node3;
	node3->next = node4;
	ListNode *result = s->removeNthFromEnd(head, 1);

	while (result != NULL) {
		cout<<result->val<<",";
		result = result->next;
	}
	cout<<endl;
	return 0;
}