int main() {
    Solution sol;

    {
        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);
        head = sol.reverseKGroup(head, 2);
        while (head != NULL) {
            cout << head->val << " ";
            head = head->next;
        }
        cout << endl;
    }

    {
        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);
        head = sol.reverseKGroup(head, 3);
        while (head != NULL) {
            cout << head->val << " ";
            head = head->next;
        }
        cout << endl;
    }
}
void main(){
    Solution s;
    
	ListNode *a = new ListNode(1);
	ListNode *b = new ListNode(2);
	ListNode *c = new ListNode(3);
	ListNode *d = new ListNode(4);
	ListNode *e = new ListNode(5);
	ListNode *f = new ListNode(6);
	a->next = b;
	b->next = c;
	c->next = d;
	d->next = e;
	e->next = f;

	a = s.reverseKGroup(a, 3);

	while(a){
		cout << a->val << ",";
		a = a->next;
	}
	cout << endl;

	system("pause");
}
Example #3
0
int main(int argc, char* argv[])
{
	int len = 10;
	struct ListNode **list = (struct ListNode **) malloc (sizeof(struct ListNode *) * len);

	for(int i = 0; i < len ; i++) {
		list[i] = (struct ListNode *) malloc (sizeof(struct ListNode));
		list[i]->val = i;
	}

	for(int i = 0; i < len-1; i++)
		list[i]->next = list[i+1];

	struct ListNode *node = list[0];

	while(node){
		printf("%d - ", node->val);
		node = node->next;
	}

	node = list[0];

	//free(list);

	Solution sol;
	sol.reverseKGroup(node, 2);

	while(node){
		printf("%d - ", node->val);
		node = node->next;
	}

}
void test()
{
	ListNode* head = CreateList({ 0,3,8,4,1,9,3 });
	Solution sol;
	ListNode* ret = sol.reverseKGroup(head, 9);
	PrintList(ret);
}
Example #5
0
int main()
{
    Solution s;

    /*
    ListNode a(1);
    ListNode b(2);

    a.next=&b;
    s.reverseKGroup(&a, 1);
    */

    /*
    ListNode a(1);
    s.reverseKGroup(&a, 2);
    */

    ListNode a(1);
    ListNode b(2);
    ListNode c(3);
    ListNode d(4);

    a.next=&b;
    b.next=&c;
    c.next=&d;

    s.reverseKGroup(&a, 2);

    std::cout<<std::endl;
    return 0;
}
Example #6
0
int main(){
	ListNode *head = new ListNode(1);
	ListNode *second = new ListNode(2);
	ListNode *third = new ListNode(3);
	ListNode *fourth = new ListNode(4);
	ListNode *fifth = new ListNode(5);
	head -> next = second;
	second -> next = third;
	third -> next = fourth;
	fourth -> next = fifth;

	ListNode *p = head;
	while(p){
		std::cout << p -> val << " ";
		p = p -> next;
	}
	std::cout << std::endl;

	Solution s;
	
	ListNode *result = s.reverseKGroup(head, 2);
	while(result){
		std::cout << result -> val << " ";
		result = result -> next;
	}
	std::cout << std::endl;
	
	return 0;
}
int main()
{
  ListNode* n0 = new ListNode(1);
  ListNode* n1 = new ListNode(2);
  ListNode* n2 = new ListNode(3);
  ListNode* n3 = new ListNode(4);
  ListNode* n4 = new ListNode(5);
  ListNode* n5 = new ListNode(6);
  ListNode* n6 = new ListNode(7);
  n0->next = n1;
  n1->next = n2;
  n2->next = n3;
  n3->next = n4;
  n4->next = n5;
  n5->next = n6;
  Solution s;
  ListNode* head = s.reverseKGroup(n0,4);
  while (head != NULL)
  {
    cout<<head->val<<"  ";
    ListNode* t = head;
    head = head->next;
    delete t;
  }
  return 0;
}
int main()
{
    //int num[] =  {1,2,3,4,5,6,7,8,9,10,11,12,32};
    int num[] = {1,2,3,4,5};
    Solution s;
    ListNode* pt, *current;
    pt = (ListNode*)malloc(sizeof(ListNode));
    current = pt;
    int i;
    for(i=0; i<sizeof(num)/sizeof(int); i++) {
        ListNode* tmp =  (ListNode*) malloc(sizeof(ListNode));
        tmp -> val = num[i];
        current  -> next  = tmp;
        current =  current -> next;
    }
    current -> next  = NULL;
    // current -> next =  pt-> next -> next -> next ->  next;
    //printList(pt -> next);

    ListNode* newList = s.reverseKGroup(pt -> next,5);
    while(NULL != newList) {
        cout <<  newList -> val << " ";
        newList =  newList -> next;
    }
    cout <<  endl;
    getchar();
    return 0;
}
Example #9
0
int main()
{
   int a[] = {2, 5, 3, 4, 6, 2, 2};
   ListNode *head = NULL, *tail = NULL;
   for (unsigned int i = 0; i < sizeof(a)/sizeof(a[0]); i++) {
      ListNode *node = new ListNode(a[i]);
      if (head == NULL) {
         head = tail = node;
      } else {
         tail->next = node;
         tail = node;
      }
   }
   ListNode *h = head;
   while (h != NULL) {
      cout << h->val << " ";
      h = h->next;
   }
   cout << endl;
   Solution s;
   head = s.reverseKGroup(head, 3);
   h = head;
   while (h != NULL) {
      cout << h->val << " ";
      h = h->next;
   }
   cout << endl;
   return 0;
}
int main(int argc, char **argv) {
	Solution solution;
	ListNode *l = solution.generate(10);
	solution.output(l);
	l = solution.reverseKGroup(l, 3);
	solution.output(l);
	return 0;
}
Example #11
0
int main()
{
    int nums[] = {1, 2, 3, 4, 5, 6};
    vector<int> nums_v(nums, nums + 4);
    Solution solution;
    ListNode *head = solution.reverseKGroup(generateList(nums_v), 3);
    printList(head);
    return 0;
}
Example #12
0
int main()
{
    Solution s;
    ListNode *h = s.reverseKGroup(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))), 1);
    while (h) {
        cout << h->val << " ";
        h = h->next;
    }
}
int main(int argc, char *argv[])
{
    ListNode *p = construct(vector<int>{1,2});
    Solution sol;
    ListNode *result = sol.reverseKGroup(p, 3);
    for (; result; result = result->next) {
        cout << result->val << " ";
    }
    cout << endl;
}
Example #14
0
int main() {
  Solution s;
  ListNode* head = new ListNode(1);
  // head->next = new ListNode(2);
  // head->next->next = new ListNode(3);
  // head->next->next->next = new ListNode(4);
  head = s.reverseKGroup(head, 2);
  printLinkList(head);
  return 0;
}
int main()
{
	int a[] = { 1, 2,3,4,5,6,7 };
	int n = 7;
	ListNode *head = init(a, n);
	print(head);
	Solution sol;
	ListNode *head1 = sol.reverseKGroup(head,4);
	print(head1);
	return 0;
}
Example #16
0
int main() {
  ListNode a(1);
  ListNode b(2);
  a.next = &b;
  Solution s;
  auto newHead = s.reverseKGroup(&a, 2);
  for (auto cur = newHead; cur != nullptr; cur = cur->next) {
    std::cout << cur->val << " ";
  }
  return 0;
}
Example #17
0
int main(){
    Solution sol;
    ListNode *head = new ListNode(0), *res = head;
    for(int i = 1 ; i <= 4 ; i++)
        res = res->next = new ListNode(i);
    auto ptr = sol.reverseKGroup(head->next, 3);
    printf("%p\n", ptr);
    while(ptr)
        printf("%d\n", ptr->val), ptr = ptr->next;
    return 0;
}
Example #18
0
int main(){
	ListNode* head = new ListNode(1);
	ListNode* tail = new ListNode(2);
	head->next = tail;
	Solution* s = new Solution();
	ListNode* newHead = s->reverseKGroup(head,2);
	while(newHead){
		printf("%d\n",newHead->val);
		newHead = newHead->next;
	}
	return 0;
}
int main() {
	Solution s;
	// 1 2 3 4 5, k = 2
	ListNode t[5];
	for (int i = 0; i < 5; i++) {
		t[i] = ListNode(i+1);
		if (i+1 < 5)
			t[i].next = &t[i+1];
	}
	print(s.reverseKGroup(&t[0], 6));
	return 0;
}
Example #20
0
int main(int argc, char ** argv){
    ListNode a(1),b(2),c(3),d(4),e(5);
    a.next = &b;b.next=&c;c.next=&d;d.next = &e;
    Solution s;
    auto ans = s.reverseKGroup(&a, 4);
    while (ans) {
        cout<<ans->val<<"-->";
        ans = ans->next;
    }
    cout<<endl;
    return 0;
}
Example #21
0
int main(int argc, char *argv[]){
	int t[] = {1, 2, 3, 4, 5, 6};
	ListNode *head = new ListNode(-1);
	ListNode *h = head;
	for(int i = 0; i < sizeof t / sizeof t[0]; i++) {
		h->next = new ListNode(t[i]);
		h = h->next;
	}
	Solution s;
	s.reverseKGroup(head->next, 2);
	return 0;
}
Example #22
0
int main()
{
    ListNode n1(1);
    ListNode n2(2);
    ListNode n3(3);
    ListNode n4(4);
    ListNode n5(5);

    n1.next = &n2;
    n2.next = &n3;
    n3.next = &n4;
    n4.next = &n5;


    ListNode m1(6);
    ListNode m2(7);
    ListNode m3(8);
    ListNode m4(9);
    ListNode m5(10);

    m1.next = &m2;
    m2.next = &m3;
    m3.next = &m4;
    m4.next = &m5;

    n5.next = &m1;



    ListNode k1(0);
    ListNode k2(0);
    ListNode k3(10);
    ListNode k4(15);
    ListNode k5(20);

    k1.next = &k2;
    k2.next = &k3;
    k3.next = &k4;
    k4.next = &k5;

    vector<ListNode *> lists;
    lists.push_back(&n1);
    lists.push_back(&m1);
    lists.push_back(&k1);

    Solution sl;
    //printList(&n1);
    //printList(&m1);
    //printList(sl.merge2Lists(&n1, &m1));
    printList(&n1);
    printList(sl.reverseKGroup(&n1, 3));
}
Example #23
0
File: 25.cpp Project: franktea/acm
int main()
{
	ListNode* l1 = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6))))));
	Solution* ps = new Solution;
	ListNode* n = ps->reverseKGroup(l1, 3);
	while(n)
	{
		cout<<n->val<<", ";
		n = n->next;
	}
	cout<<"\n";
	return 0;
}
int main()
{
	Solution s;
	ListNode *h1 = new ListNode(1), *previous = h1;
	for (int i = 2; i <= 7; i++)
	{
		ListNode *n = new ListNode(i);
		previous->next = n;
		previous = n;
	}
	print(s.reverseKGroup(h1, 2));
	return 0;
}
int main()
{
	vector<int> num = { 1,2 };
	ListNode * p = NULL;
	genListFromVec(p, num);
	Solution so;
	p = so.reverseKGroup(p, 2);
	while (p){
		cout << p->val << "->";
		p = p->next;
	}
	return 1;
}
int main()
{
	ListNode n1(1);
	ListNode n2(2);
	ListNode n3(3);
	ListNode n4(4);
	ListNode n5(5);
	int reverse_cnt;

	n1.next = &n2;
	n2.next = &n3;
	n3.next = &n4;
	n4.next = &n5;

	Solution s;

	ListNode *ret = &n1;
	ListNode *tail;

	ret = s.reverse_nth_nodes(ret, 5, &reverse_cnt, &tail);
	ret = s.reverse_nth_nodes(ret, 6, &reverse_cnt, &tail);
	ret = s.reverse_nth_nodes(ret, 2, &reverse_cnt, &tail);
	ret = s.reverse_nth_nodes(ret, 2, &reverse_cnt, &tail);
	ret = s.reverse_nth_nodes(ret, 3, &reverse_cnt, &tail);
	ret = s.reverse_nth_nodes(ret, 3, &reverse_cnt, &tail);
	ret = s.reverse_nth_nodes(ret, 4, &reverse_cnt, &tail);
	ret = s.reverse_nth_nodes(ret, 4, &reverse_cnt, &tail);

	ret = s.reverseKGroup(ret, 2);
	ret = s.reverseKGroup(ret, 2);
	ret = s.reverseKGroup(ret, 3);
	ret = s.reverseKGroup(ret, 3);
	ret = s.reverseKGroup(ret, 6);


    return 0;
}
int main () {
    vector<int> v {1,2,3,4,5,6,7,8,9};
    ListNode *head = createList(v);

    Solution s;
    head = s.reverseKGroup(head, 6);
    cout << head->val << endl;
    while (head != nullptr) {
        cout << head->val << " ";
        head = head->next;
    }
    cout << endl;

    return 0;
}
Example #28
0
int main()
{
    Solution solve;
    ListNode* Node1=new ListNode(1);
    ListNode* Node2=new ListNode(2);
    ListNode* Node3=new ListNode(3);
    ListNode* Node4=new ListNode(4);
    ListNode* Node5=new ListNode(5);
    Node1->next=Node2;
    Node2->next=Node3;
    Node3->next=Node4;
    Node4->next=Node5;
    Node1=solve.reverseKGroup(Node1,2);
    show(Node1);
    return 0;
}
int main(int argc, char *argv[])
{
   ListNode *head = new ListNode(1);
   head->next = new ListNode(2);

   Solution slt;
   head = slt.reverseKGroup(head, 2);

   while(head)
   {
      cout << head->val << endl;
      head = head->next;
   }
   
   return 0;
}
Example #30
0
int main(int argc, char const *argv[])
{
    Solution s;
    using ListNode=Solution::ListNode;
    ListNode a3[] = { -2, -1, 0, 2 };
    for (auto i = begin(a3); i < end(a3) - 1; ++i)
    {
        i->next = i + 1;
    }
    auto result=s.reverseKGroup(a3,3);
    while (result != NULL)
    {
        cout << result->val << " ";
        result = result->next;
    }
    return 0;
}