#include <iostream>
#include <stack>
using namespace std;
struct ListNode
{
int m_nValue;
ListNode * m_pNext;
};
ListNode * addToTail (ListNode * pHead, int value)
{
ListNode * pNode = new ListNode();
pNode ->m_nValue = value;
pNode ->m_pNext = NULL;
if((pHead) == NULL)
{
( pHead) = pNode;
}
else
{
ListNode * pNew = pHead;
while( pNew ->m_pNext != NULL)
{
pNew = pNew->m_pNext;
}
pNew->m_pNext = pNode;
}
return pHead;
}
ListNode * addToHead(ListNode * pHead, int value)
{
if(pHead == NULL)
{
cout<<"null"<<endl;
}
ListNode * pNode = pHead;
ListNode * pNew = new ListNode();
pNew ->m_nValue = value;
pNew ->m_pNext = pNode;
pHead = pNew;
return pHead;
}
ListNode * addToSortList(ListNode * pHead, int value)
{
ListNode * pNew = new ListNode();
pNew ->m_nValue = value;
pNew ->m_pNext = NULL;
if( pHead ->m_nValue > value)
{
pNew ->m_pNext = pHead;
pHead = pNew;
}
else {
ListNode * pNode = pHead;
while( pNode ->m_pNext != NULL &&
pNode ->m_pNext ->m_nValue < value
)
{
pNode = pNode ->m_pNext;
}
pNew->m_pNext = pNode ->m_pNext;
pNode ->m_pNext = pNew;
}
return pHead;
}
void printList(ListNode * pHead ,int k)
{
stack<ListNode *> stacks;
ListNode * pNode = pHead;
int count = k;
while(pNode != NULL )
{
count = k;
while( pNode != NULL &&( count --))
{
stacks.push(pNode);
pNode = pNode ->m_pNext;
}
while( !stacks.empty())
{
ListNode * temp = stacks.top();
cout<<temp->m_nValue<<"------";
stacks.pop();
}
}
cout <<endl;
}
void printListRecursively(ListNode * pHead)
{
if(pHead != NULL)
{
if(pHead ->m_pNext != NULL)
{
printListRecursively(pHead ->m_pNext);
}
cout <<pHead ->m_nValue<<"------";
}
}
void printList( ListNode * pHead)
{
ListNode * pNode = pHead;
while(pNode != NULL )
{
cout <<pNode->m_nValue<<"----";
pNode = pNode->m_pNext;
}
cout <<endl;
}
ListNode * removeListHeadNode(ListNode * pHead)
{
if(pHead == NULL)
return NULL;
if( pHead ->m_pNext == NULL)
{
pHead =pHead->m_pNext;
return NULL;
}
else
{
ListNode * pToDelete = pHead;
pHead = pHead ->m_pNext;
delete pToDelete;
pToDelete = NULL;
return pHead;
}
}
void removeListNode(ListNode * pHead, int value)
{
if( pHead == NULL)
return;
ListNode * pToDelete = NULL;
if(pHead->m_nValue == value)
{
pToDelete = pHead;
pHead = pHead ->m_pNext;
}
else
{
ListNode * pNode = pHead;
while(pNode ->m_pNext->m_nValue != value && pNode ->m_pNext != NULL)
{
pNode = pNode ->m_pNext;
}
if( pNode ->m_pNext->m_nValue == value && pNode ->m_pNext != NULL)
{
pToDelete = pNode->m_pNext;
pNode ->m_pNext = pNode ->m_pNext->m_pNext;
}
}
if( pToDelete != NULL)
{
delete pToDelete;
pToDelete = NULL;
}
}
ListNode * reverseList(ListNode * pHead)
{
ListNode * curr = pHead;
ListNode * pre = NULL;
while(curr != NULL)
{
ListNode * pNext = curr ->m_pNext;
curr->m_pNext = pre;
pre = curr;
curr = pNext;
}
return pre;
}
ListNode * reverseList( ListNode * pHead, int k)
{
ListNode * pre = NULL;
ListNode * curr = pHead;
ListNode * reverseHead = NULL;
stack<ListNode * > stacks;
int count = k;
while(curr != NULL)
{
count = k;
while(count -- && curr != NULL)
{
stacks.push(curr);
curr= curr->m_pNext;
}
while( !stacks.empty())
{
ListNode * temp = stacks.top();
stacks.pop();
}
}
return pre;
}
ListNode * FindKthToTail(ListNode * pHead, unsigned int k )
{
if( pHead == NULL || k == 0)
{
return NULL;
}
ListNode * pAhead = pHead;
ListNode * pBehind = NULL;
for(unsigned int i = 0 ; i < k-1; i++)
{
if( pAhead ->m_pNext != NULL)
{
pAhead = pAhead ->m_pNext;
}
else
{
return NULL;
}
}
pBehind = pHead;
while( pAhead->m_pNext!= NULL)
{
pAhead = pAhead ->m_pNext;
pBehind = pBehind ->m_pNext;
}
//cout<<endl<<pBehind->m_nValue<<endl;
return pBehind;
}
ListNode * MergeList(ListNode * pHead1, ListNode * pHead2)
{
if(pHead1 == NULL)
{
return pHead2;
}
else if(pHead2 == NULL)
{
return pHead1;
}
ListNode * mergedHead = NULL;
if( pHead1->m_nValue < pHead2 ->m_nValue)
{
mergedHead = pHead1;
mergedHead ->m_pNext = (MergeList(pHead1->m_pNext, pHead2));
}else
{
mergedHead = pHead2;
mergedHead ->m_pNext = (MergeList(pHead1, pHead2->m_pNext));
}
return mergedHead;
}
int main()
{
// ListNode * pHead = new ListNode();
// pHead= NULL;
ListNode * pHead = NULL;
int length = 10;
for(int i = 0 ; i < length ; ++i)
{
pHead = addToHead(pHead, 2*i+1);
}
printList(pHead);
cout <<"_________________________"<<endl;
pHead = reverseList(pHead);
printList(pHead);
cout <<"_________________________"<<endl;
ListNode * pTest = NULL;
for(int i = 0 ; i < length ; ++i)
{
pTest = addToHead(pTest,2*i);
}
pTest = reverseList(pTest);
printList(pTest);
cout <<"_________________________"<<endl;
pTest = MergeList(pHead, pTest);
printList(pTest);
// cout <<FindKthToTail(pHead,3)->m_nValue<<endl;
// pHead = reverseList(pHead, 3);
//printList(pHead);
//printList(pHead, 2);
cout <<"_________________________"<<endl;
// printListRecursively(pHead);
cout<<endl;
/*
pHead = reverseList(pHead);
printList(pHead);
pHead = addToSortList(pHead, 10);
printList(pHead);
pHead = removeListHeadNode(pHead);
printList(pHead);
*/
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
struct ListNode
{
int m_nValue;
ListNode * m_pNext;
};
void addToTail (ListNode ** pHead, int value)
{
ListNode * pNode = new ListNode();
pNode ->m_nValue = value;
pNode ->m_pNext = NULL;
if((*pHead) == NULL)
{
(* pHead) = pNode;
}
else
{
ListNode * pNew = *pHead;
while( pNew ->m_pNext != NULL)
{
pNew = pNew->m_pNext;
}
pNew->m_pNext = pNode;
}
}
void printList( ListNode ** pHead)
{
ListNode * pNode = *pHead;
while(pNode != NULL )
{
cout <<pNode->m_nValue<<"----";
pNode = pNode->m_pNext;
}
cout <<endl;
}
//________________________________
ListNode * addToTail (ListNode * pHead, int value)
{
ListNode * pNode = new ListNode();
pNode ->m_nValue = value;
pNode ->m_pNext = NULL;
if((pHead) == NULL)
{
( pHead) = pNode;
}
else
{
ListNode * pNew = pHead;
while( pNew ->m_pNext != NULL)
{
pNew = pNew->m_pNext;
}
pNew->m_pNext = pNode;
}
return pHead;
}
ListNode * addToHead(ListNode * pHead, int value)
{
if(pHead == NULL)
{
cout<<"null"<<endl;
}
ListNode * pNode = pHead;
ListNode * pNew = new ListNode();
pNew ->m_nValue = value;
pNew ->m_pNext = pNode;
pHead = pNew;
return pHead;
}
ListNode * addToSortList(ListNode * pHead, int value)
{
ListNode * pNew = new ListNode();
pNew ->m_nValue = value;
pNew ->m_pNext = NULL;
if( pHead ->m_nValue > value)
{
pNew ->m_pNext = pHead;
pHead = pNew;
}
else {
ListNode * pNode = pHead;
while( pNode ->m_pNext != NULL &&
pNode ->m_pNext ->m_nValue < value
)
{
pNode = pNode ->m_pNext;
}
pNew->m_pNext = pNode ->m_pNext;
pNode ->m_pNext = pNew;
}
return pHead;
}
void printList(ListNode * pHead ,int k)
{
stack<ListNode *> stacks;
ListNode * pNode = pHead;
int count = k;
while(pNode != NULL )
{
count = k;
while( pNode != NULL &&( count --))
{
stacks.push(pNode);
pNode = pNode ->m_pNext;
}
while( !stacks.empty())
{
ListNode * temp = stacks.top();
cout<<temp->m_nValue<<"------";
stacks.pop();
}
}
cout <<endl;
}
void printListRecursively(ListNode * pHead)
{
if(pHead != NULL)
{
if(pHead ->m_pNext != NULL)
{
printListRecursively(pHead ->m_pNext);
}
cout <<pHead ->m_nValue<<"------";
}
}
void printList( ListNode * pHead)
{
ListNode * pNode = pHead;
while(pNode != NULL )
{
cout <<pNode->m_nValue<<"----";
pNode = pNode->m_pNext;
}
cout <<endl;
}
ListNode * removeListHeadNode(ListNode * pHead)
{
if(pHead == NULL)
return NULL;
if( pHead ->m_pNext == NULL)
{
pHead =pHead->m_pNext;
return NULL;
}
else
{
ListNode * pToDelete = pHead;
pHead = pHead ->m_pNext;
delete pToDelete;
pToDelete = NULL;
return pHead;
}
}
void removeListNode(ListNode * pHead, int value)
{
if( pHead == NULL)
return;
ListNode * pToDelete = NULL;
if(pHead->m_nValue == value)
{
pToDelete = pHead;
pHead = pHead ->m_pNext;
}
else
{
ListNode * pNode = pHead;
while(pNode ->m_pNext->m_nValue != value && pNode ->m_pNext != NULL)
{
pNode = pNode ->m_pNext;
}
if( pNode ->m_pNext->m_nValue == value && pNode ->m_pNext != NULL)
{
pToDelete = pNode->m_pNext;
pNode ->m_pNext = pNode ->m_pNext->m_pNext;
}
}
if( pToDelete != NULL)
{
delete pToDelete;
pToDelete = NULL;
}
}
ListNode * reverseList(ListNode * pHead)
{
ListNode * curr = pHead;
ListNode * pre = NULL;
while(curr != NULL)
{
ListNode * pNext = curr ->m_pNext;
curr->m_pNext = pre;
pre = curr;
curr = pNext;
}
return pre;
}
ListNode * reverseList( ListNode * pHead, int k)
{
ListNode * pre = NULL;
ListNode * curr = pHead;
ListNode * reverseHead = NULL;
stack<ListNode * > stacks;
int count = k;
while(curr != NULL)
{
count = k;
while(count -- && curr != NULL)
{
stacks.push(curr);
curr= curr->m_pNext;
}
while( !stacks.empty())
{
ListNode * temp = stacks.top();
stacks.pop();
}
}
return pre;
}
int TTmain()
{
cout << "Hello world!" << endl;
ListNode * head =new ListNode();
// head ->m_nValue = 0;
// head ->m_pNext = NULL;
//printList(head);
for(int i = 1 ; i < 10; i++)
{
addToTail(head,i );
}
printList(head);
// removeListNode(head, 3);
printList(head);
head = reverseList(head);
printList(head);
/*
head = reverseList(head, 3);
printList(head);
*/
// ListNode * Ghead = new ListNode();
// Ghead ->m_nValue = 10;
// Ghead ->m_pNext = NULL;
// printList(Ghead);
return 0;
}
ListNode * FindKthToTail(ListNode * pHead, unsigned int k )
{
if( pHead == NULL || k == 0)
{
return NULL;
}
ListNode * pAhead = pHead;
ListNode * pBehind = NULL;
for(unsigned int i = 0 ; i < k-1; i++)
{
if( pAhead ->m_pNext != NULL)
{
pAhead = pAhead ->m_pNext;
}
else
{
return NULL;
}
}
pBehind = pHead;
while( pAhead->m_pNext!= NULL)
{
pAhead = pAhead ->m_pNext;
pBehind = pBehind ->m_pNext;
}
//cout<<endl<<pBehind->m_nValue<<endl;
return pBehind;
}
ListNode * MergeList(ListNode * pHead1, ListNode * pHead2)
{
if(pHead1 == NULL)
{
return pHead2;
}
else if(pHead2 == NULL)
{
return pHead1;
}
ListNode * mergedHead = NULL;
if( pHead1->m_nValue < pHead2 ->m_nValue)
{
mergedHead = pHead1;
mergedHead ->m_pNext = (MergeList(pHead1->m_pNext, pHead2));
}else
{
mergedHead = pHead2;
mergedHead ->m_pNext = (MergeList(pHead1, pHead2->m_pNext));
}
return mergedHead;
}
int main()
{
// ListNode * pHead = new ListNode();
// pHead= NULL;
ListNode * pHead = NULL;
int length = 10;
for(int i = 0 ; i < length ; ++i)
{
pHead = addToHead(pHead, 2*i+1);
}
printList(pHead);
cout <<"_________________________"<<endl;
pHead = reverseList(pHead);
printList(pHead);
cout <<"_________________________"<<endl;
ListNode * pTest = NULL;
for(int i = 0 ; i < length ; ++i)
{
pTest = addToHead(pTest,2*i);
}
pTest = reverseList(pTest);
printList(pTest);
cout <<"_________________________"<<endl;
pTest = MergeList(pHead, pTest);
printList(pTest);
// cout <<FindKthToTail(pHead,3)->m_nValue<<endl;
// pHead = reverseList(pHead, 3);
//printList(pHead);
//printList(pHead, 2);
cout <<"_________________________"<<endl;
// printListRecursively(pHead);
cout<<endl;
/*
pHead = reverseList(pHead);
printList(pHead);
pHead = addToSortList(pHead, 10);
printList(pHead);
pHead = removeListHeadNode(pHead);
printList(pHead);
*/
return 0;
}
#include <stack>
using namespace std;
struct ListNode
{
int m_nValue;
ListNode * m_pNext;
};
ListNode * addToTail (ListNode * pHead, int value)
{
ListNode * pNode = new ListNode();
pNode ->m_nValue = value;
pNode ->m_pNext = NULL;
if((pHead) == NULL)
{
( pHead) = pNode;
}
else
{
ListNode * pNew = pHead;
while( pNew ->m_pNext != NULL)
{
pNew = pNew->m_pNext;
}
pNew->m_pNext = pNode;
}
return pHead;
}
ListNode * addToHead(ListNode * pHead, int value)
{
if(pHead == NULL)
{
cout<<"null"<<endl;
}
ListNode * pNode = pHead;
ListNode * pNew = new ListNode();
pNew ->m_nValue = value;
pNew ->m_pNext = pNode;
pHead = pNew;
return pHead;
}
ListNode * addToSortList(ListNode * pHead, int value)
{
ListNode * pNew = new ListNode();
pNew ->m_nValue = value;
pNew ->m_pNext = NULL;
if( pHead ->m_nValue > value)
{
pNew ->m_pNext = pHead;
pHead = pNew;
}
else {
ListNode * pNode = pHead;
while( pNode ->m_pNext != NULL &&
pNode ->m_pNext ->m_nValue < value
)
{
pNode = pNode ->m_pNext;
}
pNew->m_pNext = pNode ->m_pNext;
pNode ->m_pNext = pNew;
}
return pHead;
}
void printList(ListNode * pHead ,int k)
{
stack<ListNode *> stacks;
ListNode * pNode = pHead;
int count = k;
while(pNode != NULL )
{
count = k;
while( pNode != NULL &&( count --))
{
stacks.push(pNode);
pNode = pNode ->m_pNext;
}
while( !stacks.empty())
{
ListNode * temp = stacks.top();
cout<<temp->m_nValue<<"------";
stacks.pop();
}
}
cout <<endl;
}
void printListRecursively(ListNode * pHead)
{
if(pHead != NULL)
{
if(pHead ->m_pNext != NULL)
{
printListRecursively(pHead ->m_pNext);
}
cout <<pHead ->m_nValue<<"------";
}
}
void printList( ListNode * pHead)
{
ListNode * pNode = pHead;
while(pNode != NULL )
{
cout <<pNode->m_nValue<<"----";
pNode = pNode->m_pNext;
}
cout <<endl;
}
ListNode * removeListHeadNode(ListNode * pHead)
{
if(pHead == NULL)
return NULL;
if( pHead ->m_pNext == NULL)
{
pHead =pHead->m_pNext;
return NULL;
}
else
{
ListNode * pToDelete = pHead;
pHead = pHead ->m_pNext;
delete pToDelete;
pToDelete = NULL;
return pHead;
}
}
void removeListNode(ListNode * pHead, int value)
{
if( pHead == NULL)
return;
ListNode * pToDelete = NULL;
if(pHead->m_nValue == value)
{
pToDelete = pHead;
pHead = pHead ->m_pNext;
}
else
{
ListNode * pNode = pHead;
while(pNode ->m_pNext->m_nValue != value && pNode ->m_pNext != NULL)
{
pNode = pNode ->m_pNext;
}
if( pNode ->m_pNext->m_nValue == value && pNode ->m_pNext != NULL)
{
pToDelete = pNode->m_pNext;
pNode ->m_pNext = pNode ->m_pNext->m_pNext;
}
}
if( pToDelete != NULL)
{
delete pToDelete;
pToDelete = NULL;
}
}
ListNode * reverseList(ListNode * pHead)
{
ListNode * curr = pHead;
ListNode * pre = NULL;
while(curr != NULL)
{
ListNode * pNext = curr ->m_pNext;
curr->m_pNext = pre;
pre = curr;
curr = pNext;
}
return pre;
}
ListNode * reverseList( ListNode * pHead, int k)
{
ListNode * pre = NULL;
ListNode * curr = pHead;
ListNode * reverseHead = NULL;
stack<ListNode * > stacks;
int count = k;
while(curr != NULL)
{
count = k;
while(count -- && curr != NULL)
{
stacks.push(curr);
curr= curr->m_pNext;
}
while( !stacks.empty())
{
ListNode * temp = stacks.top();
stacks.pop();
}
}
return pre;
}
ListNode * FindKthToTail(ListNode * pHead, unsigned int k )
{
if( pHead == NULL || k == 0)
{
return NULL;
}
ListNode * pAhead = pHead;
ListNode * pBehind = NULL;
for(unsigned int i = 0 ; i < k-1; i++)
{
if( pAhead ->m_pNext != NULL)
{
pAhead = pAhead ->m_pNext;
}
else
{
return NULL;
}
}
pBehind = pHead;
while( pAhead->m_pNext!= NULL)
{
pAhead = pAhead ->m_pNext;
pBehind = pBehind ->m_pNext;
}
//cout<<endl<<pBehind->m_nValue<<endl;
return pBehind;
}
ListNode * MergeList(ListNode * pHead1, ListNode * pHead2)
{
if(pHead1 == NULL)
{
return pHead2;
}
else if(pHead2 == NULL)
{
return pHead1;
}
ListNode * mergedHead = NULL;
if( pHead1->m_nValue < pHead2 ->m_nValue)
{
mergedHead = pHead1;
mergedHead ->m_pNext = (MergeList(pHead1->m_pNext, pHead2));
}else
{
mergedHead = pHead2;
mergedHead ->m_pNext = (MergeList(pHead1, pHead2->m_pNext));
}
return mergedHead;
}
int main()
{
// ListNode * pHead = new ListNode();
// pHead= NULL;
ListNode * pHead = NULL;
int length = 10;
for(int i = 0 ; i < length ; ++i)
{
pHead = addToHead(pHead, 2*i+1);
}
printList(pHead);
cout <<"_________________________"<<endl;
pHead = reverseList(pHead);
printList(pHead);
cout <<"_________________________"<<endl;
ListNode * pTest = NULL;
for(int i = 0 ; i < length ; ++i)
{
pTest = addToHead(pTest,2*i);
}
pTest = reverseList(pTest);
printList(pTest);
cout <<"_________________________"<<endl;
pTest = MergeList(pHead, pTest);
printList(pTest);
// cout <<FindKthToTail(pHead,3)->m_nValue<<endl;
// pHead = reverseList(pHead, 3);
//printList(pHead);
//printList(pHead, 2);
cout <<"_________________________"<<endl;
// printListRecursively(pHead);
cout<<endl;
/*
pHead = reverseList(pHead);
printList(pHead);
pHead = addToSortList(pHead, 10);
printList(pHead);
pHead = removeListHeadNode(pHead);
printList(pHead);
*/
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
struct ListNode
{
int m_nValue;
ListNode * m_pNext;
};
void addToTail (ListNode ** pHead, int value)
{
ListNode * pNode = new ListNode();
pNode ->m_nValue = value;
pNode ->m_pNext = NULL;
if((*pHead) == NULL)
{
(* pHead) = pNode;
}
else
{
ListNode * pNew = *pHead;
while( pNew ->m_pNext != NULL)
{
pNew = pNew->m_pNext;
}
pNew->m_pNext = pNode;
}
}
void printList( ListNode ** pHead)
{
ListNode * pNode = *pHead;
while(pNode != NULL )
{
cout <<pNode->m_nValue<<"----";
pNode = pNode->m_pNext;
}
cout <<endl;
}
//________________________________
ListNode * addToTail (ListNode * pHead, int value)
{
ListNode * pNode = new ListNode();
pNode ->m_nValue = value;
pNode ->m_pNext = NULL;
if((pHead) == NULL)
{
( pHead) = pNode;
}
else
{
ListNode * pNew = pHead;
while( pNew ->m_pNext != NULL)
{
pNew = pNew->m_pNext;
}
pNew->m_pNext = pNode;
}
return pHead;
}
ListNode * addToHead(ListNode * pHead, int value)
{
if(pHead == NULL)
{
cout<<"null"<<endl;
}
ListNode * pNode = pHead;
ListNode * pNew = new ListNode();
pNew ->m_nValue = value;
pNew ->m_pNext = pNode;
pHead = pNew;
return pHead;
}
ListNode * addToSortList(ListNode * pHead, int value)
{
ListNode * pNew = new ListNode();
pNew ->m_nValue = value;
pNew ->m_pNext = NULL;
if( pHead ->m_nValue > value)
{
pNew ->m_pNext = pHead;
pHead = pNew;
}
else {
ListNode * pNode = pHead;
while( pNode ->m_pNext != NULL &&
pNode ->m_pNext ->m_nValue < value
)
{
pNode = pNode ->m_pNext;
}
pNew->m_pNext = pNode ->m_pNext;
pNode ->m_pNext = pNew;
}
return pHead;
}
void printList(ListNode * pHead ,int k)
{
stack<ListNode *> stacks;
ListNode * pNode = pHead;
int count = k;
while(pNode != NULL )
{
count = k;
while( pNode != NULL &&( count --))
{
stacks.push(pNode);
pNode = pNode ->m_pNext;
}
while( !stacks.empty())
{
ListNode * temp = stacks.top();
cout<<temp->m_nValue<<"------";
stacks.pop();
}
}
cout <<endl;
}
void printListRecursively(ListNode * pHead)
{
if(pHead != NULL)
{
if(pHead ->m_pNext != NULL)
{
printListRecursively(pHead ->m_pNext);
}
cout <<pHead ->m_nValue<<"------";
}
}
void printList( ListNode * pHead)
{
ListNode * pNode = pHead;
while(pNode != NULL )
{
cout <<pNode->m_nValue<<"----";
pNode = pNode->m_pNext;
}
cout <<endl;
}
ListNode * removeListHeadNode(ListNode * pHead)
{
if(pHead == NULL)
return NULL;
if( pHead ->m_pNext == NULL)
{
pHead =pHead->m_pNext;
return NULL;
}
else
{
ListNode * pToDelete = pHead;
pHead = pHead ->m_pNext;
delete pToDelete;
pToDelete = NULL;
return pHead;
}
}
void removeListNode(ListNode * pHead, int value)
{
if( pHead == NULL)
return;
ListNode * pToDelete = NULL;
if(pHead->m_nValue == value)
{
pToDelete = pHead;
pHead = pHead ->m_pNext;
}
else
{
ListNode * pNode = pHead;
while(pNode ->m_pNext->m_nValue != value && pNode ->m_pNext != NULL)
{
pNode = pNode ->m_pNext;
}
if( pNode ->m_pNext->m_nValue == value && pNode ->m_pNext != NULL)
{
pToDelete = pNode->m_pNext;
pNode ->m_pNext = pNode ->m_pNext->m_pNext;
}
}
if( pToDelete != NULL)
{
delete pToDelete;
pToDelete = NULL;
}
}
ListNode * reverseList(ListNode * pHead)
{
ListNode * curr = pHead;
ListNode * pre = NULL;
while(curr != NULL)
{
ListNode * pNext = curr ->m_pNext;
curr->m_pNext = pre;
pre = curr;
curr = pNext;
}
return pre;
}
ListNode * reverseList( ListNode * pHead, int k)
{
ListNode * pre = NULL;
ListNode * curr = pHead;
ListNode * reverseHead = NULL;
stack<ListNode * > stacks;
int count = k;
while(curr != NULL)
{
count = k;
while(count -- && curr != NULL)
{
stacks.push(curr);
curr= curr->m_pNext;
}
while( !stacks.empty())
{
ListNode * temp = stacks.top();
stacks.pop();
}
}
return pre;
}
int TTmain()
{
cout << "Hello world!" << endl;
ListNode * head =new ListNode();
// head ->m_nValue = 0;
// head ->m_pNext = NULL;
//printList(head);
for(int i = 1 ; i < 10; i++)
{
addToTail(head,i );
}
printList(head);
// removeListNode(head, 3);
printList(head);
head = reverseList(head);
printList(head);
/*
head = reverseList(head, 3);
printList(head);
*/
// ListNode * Ghead = new ListNode();
// Ghead ->m_nValue = 10;
// Ghead ->m_pNext = NULL;
// printList(Ghead);
return 0;
}
ListNode * FindKthToTail(ListNode * pHead, unsigned int k )
{
if( pHead == NULL || k == 0)
{
return NULL;
}
ListNode * pAhead = pHead;
ListNode * pBehind = NULL;
for(unsigned int i = 0 ; i < k-1; i++)
{
if( pAhead ->m_pNext != NULL)
{
pAhead = pAhead ->m_pNext;
}
else
{
return NULL;
}
}
pBehind = pHead;
while( pAhead->m_pNext!= NULL)
{
pAhead = pAhead ->m_pNext;
pBehind = pBehind ->m_pNext;
}
//cout<<endl<<pBehind->m_nValue<<endl;
return pBehind;
}
ListNode * MergeList(ListNode * pHead1, ListNode * pHead2)
{
if(pHead1 == NULL)
{
return pHead2;
}
else if(pHead2 == NULL)
{
return pHead1;
}
ListNode * mergedHead = NULL;
if( pHead1->m_nValue < pHead2 ->m_nValue)
{
mergedHead = pHead1;
mergedHead ->m_pNext = (MergeList(pHead1->m_pNext, pHead2));
}else
{
mergedHead = pHead2;
mergedHead ->m_pNext = (MergeList(pHead1, pHead2->m_pNext));
}
return mergedHead;
}
int main()
{
// ListNode * pHead = new ListNode();
// pHead= NULL;
ListNode * pHead = NULL;
int length = 10;
for(int i = 0 ; i < length ; ++i)
{
pHead = addToHead(pHead, 2*i+1);
}
printList(pHead);
cout <<"_________________________"<<endl;
pHead = reverseList(pHead);
printList(pHead);
cout <<"_________________________"<<endl;
ListNode * pTest = NULL;
for(int i = 0 ; i < length ; ++i)
{
pTest = addToHead(pTest,2*i);
}
pTest = reverseList(pTest);
printList(pTest);
cout <<"_________________________"<<endl;
pTest = MergeList(pHead, pTest);
printList(pTest);
// cout <<FindKthToTail(pHead,3)->m_nValue<<endl;
// pHead = reverseList(pHead, 3);
//printList(pHead);
//printList(pHead, 2);
cout <<"_________________________"<<endl;
// printListRecursively(pHead);
cout<<endl;
/*
pHead = reverseList(pHead);
printList(pHead);
pHead = addToSortList(pHead, 10);
printList(pHead);
pHead = removeListHeadNode(pHead);
printList(pHead);
*/
return 0;
}