void walklist(node *head)
{
	node *slow = head->next;//使用快慢指针
	node *fast = head->next;
	node *preslow = slow;//slow用于处理节点个数奇偶数的问题

	if(fast->next == NULL)
	{
		return;
	}
	//fast = fast->next->next;
	while(fast->next != NULL)
	{
		fast = fast->next->next;
		preslow = slow;
		if(fast == NULL)
		{
			break;
		}
		slow = slow->next;
	}

	//if(fast != NULL)
	//{
		//printf("%d\n", slow->key);
		//printf("%d\n", slow->next->key);
	//}
	//else
	//{
		printf("%d\n", preslow->key);
		printf("%d\n", preslow->next->key);
	//}
	
	//node *tail = preslow;
	node *mid = NULL;
	if(fast != NULL)
	{
		mid = reverselist(slow);//奇数个节点,slow相当于头节点
	}
	else
	{
		mid = reverselist(preslow);//偶数个节点
	}
	printlinkedlist(mid);

	node *p = head->next;
	mid = mid->next;
	while(mid != NULL)
	{
		printf("%d-%d\n", p->key, mid->key);
		if(p->key != mid->key)
		{
			printf("NO\n");
			return;
		}
		p = p->next;
		mid = mid->next;
	}
	printf("YES\n");
}
Exemplo n.º 2
0
 bool isPalindrome(ListNode* head) {
   ListNode *slow=head,*fast=head;
   ListNode *pre;
   while(fast && fast->next){
       pre=slow;
       slow=slow->next;
       fast=fast->next->next;
   }
   
   //split into two list
   pre->next=NULL;
   ListNode *newhead;
   if(fast==NULL){
        //even number list node
       newhead=reverselist(slow);
   }else{
        //odd number list node
       newhead=reverselist(slow->next);
   }
   
   while(head && newhead){
       if(head->val != newhead->val){
           return false;
       }
       head=head->next;
       newhead=newhead->next;
   }
   
   return true;
 }
Exemplo n.º 3
0
Arquivo: main.cpp Projeto: uniquews/LC
 ListNode *swapPairs(ListNode *head) {
     if (head == nullptr) {
         return nullptr;
     }
     
     ListNode dummy(-1);
     ListNode *prepare = &dummy;
     dummy.next = head;
     
     int len = 0;
     ListNode *cur = head;
     while (cur != nullptr) {
         len++;
         cur = cur->next;
     }
     
     
     for (int i = 0; i < len; i = i + 2) {
         if (len - i < 2) {
             break;
         }
         
         prepare = reverselist(prepare);
     }
     
     return dummy.next;
 }
void main()
{
        NODE header,rev;
        header=createempty();
        getinputs(5,header);
        displaylist(header);
        rev=reverselist(header);
}
Exemplo n.º 5
0
int main()
{
    Node* head;
    head=createlist();
    assert(head!=NULL);
    printlist(head);
    reverselist(&head);
    printf("after reverse\n");
    printlist(head);
    
    return 0;
}
Exemplo n.º 6
0
t_ls	*sorta(t_ls *a, int *state)
{
	if (state[F] == 1)
		return (reverselist(a));
	else if (state[T] == 0 && state[R] == 0)
		sortlist(&a, classalpha);
	else if (state[T] == 0 && state[R] == 1)
		sortlist(&a, classalpharev);
	else if (state[T] == 1 && state[R] == 0 && state[U] == 1)
		sortlist(&a, classtimeaccess);
	else if (state[T] == 1 && state[R] == 1 && state[U] == 1)
		sortlist(&a, classtimeaccessr);
	else if (state[T] == 1 && state[R] == 0)
		sortlist(&a, classtime);
	else if (state[T] == 1 && state[R] == 1)
		sortlist(&a, classtimerev);
	return (a);
}
Exemplo n.º 7
0
int main(int argc, char const *argv[])
{
	struct ListNode *use;
	struct ListNode *temp;
	use = makelist();
	for (temp = use; temp != NULL ; temp = temp->m_pNext){
		printf("%d-->",temp->m_nkey);
	}
	printf("\n");

	temp = reverselist(use);
	for (; temp != NULL ; temp = temp->m_pNext){
		printf("%d-->",temp->m_nkey);
	}
	printf("\n");


	return 0;
}