本文整理汇总了C++中reverseList函数的典型用法代码示例。如果您正苦于以下问题:C++ reverseList函数的具体用法?C++ reverseList怎么用?C++ reverseList使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了reverseList函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testReverse
void testReverse(void){
printf("Testing reverse!\n");
//reverse the list once...
List l = setUpList();
printList(l);
reverseList(l);
printList(l);
assert(getElement(l, 0) == 4);
assert(getElement(l, 1) == 7);
assert(getElement(l, 2) == 8);
assert(getElement(l, 3) == 1);
assert(listLength(l) == START_LENGTH);
//reverse it again!
reverseList(l);
printList(l);
assert(getElement(l, 0) == 1);
assert(getElement(l, 1) == 8);
assert(getElement(l, 2) == 7);
assert(getElement(l, 3) == 4);
assert(listLength(l) == START_LENGTH);
//testing that appended items are reversed as well
append(l, 32);
reverseList(l);
assert(getElement(l, 0) == 32);
assert(listLength(l) == START_LENGTH + 1);
printf("Reversing passed!\n");
}
示例2: testReverseList
void testReverseList() {
cout << endl;
cout << "Test reverseList():" << endl;
cout << "===================" << endl;
char a[] = "a";
char b[] = "b";
char c[] = "c";
char d[] = "d";
char e[] = "e";
char f[] = "f";
char g[] = "g";
Node<char*> head;
head.data = a;
head.next = nullptr;
appendToList(&head, &b[0]);
appendToList(&head, &c[0]);
appendToList(&head, &d[0]);
appendToList(&head, &e[0]);
appendToList(&head, &f[0]);
appendToList(&head, &g[0]);
printList(&head);
Node<char*>* rev = reverseList(&head);
printList(rev);
Node<char*>* orig = reverseList(rev);
printList(orig);
}
示例3: main
int main(int argc, char **argv)
{
int i;
int n;
NodePtr node;
JobPtr job;
ListPtr list;
if (argc != 2) {
fprintf(stderr, "Usage: %s <list size> \n",argv[0]);
exit(1);
}
n = atoi(argv[1]);
list = createList();
for (i=0; i<n; i++) {
job = createJob(i, "cmd args");
node = createNode(job);
addAtFront(list, node);
}
printList(list);
reverseList(list);
printList(list);
reverseList(list);
printList(list);
exit(0);
}
示例4: reverseList
ListNode *reverseBetween(ListNode *head, int m, int n) {
if (head == NULL)
return NULL;
if (head->next == NULL || m == n)
return head;
m--; n--;
if (m == 0)
{
head = reverseList(head, n - m + 1);
return head;
}
int c = 0;
ListNode* curNode = head;
ListNode* tail = NULL;
while (c < m)
{
assert(curNode);
tail = curNode;
curNode = curNode->next;
c++;
}
curNode = reverseList(curNode, n - m + 1);
tail->next = curNode;
return head;
}
示例5: isPalindrome2
// O(n)-time, O(1)-space
bool isPalindrome2(ListNode *head) {
if (head == NULL) {
return true;
}
// 计算中间节点
ListNode *mid = findMiddleNode(head);
// 反转后半段链表
ListNode *reverseHead = reverseList(mid->next);
// 连接前后两半段链表
mid->next = reverseHead;
// 比较前后两个半段链表
// 注意:后半段链表节点个数 <= 前半段链表节点个数
bool isPal = true;
while (reverseHead != NULL) {
if (head->val != reverseHead->val) {
isPal = false;
}
head = head->next;
reverseHead = reverseHead->next;
}
// 恢复原链表
reverseHead = reverseList(mid->next);
mid->next = reverseHead;
return isPal;
}
示例6: delLesserNodes
/* Deletes nodes which have a node with greater value node on left side */
void delLesserNodes(struct Node **head_ref)
{
/* 1) Reverse the linked list */
reverseList(head_ref);
/* 2) In the reversed list, delete nodes which have a node with greater value node on left side.
Note that head node is never deleted because it is the leftmost node.*/
_delLesserNodes(*head_ref);
/* 3) Reverse the linked list again to retain the original order */
reverseList(head_ref);
}
示例7: linkedListTest
void linkedListTest() {
node * list = nullptr;
print(list);
std::cout << "↔";
reverseList(list);
print(list);
for (const auto & elem : {5,1,3,2,4,8,6,9,7}) {
appendList(list, elem);
print(list);
std::cout << "↔";
reverseList(list);
print(list);
}
}
示例8: isPalindrome
bool isPalindrome(ListNode* head) {
if (head == nullptr || head->next == nullptr) return true;
ListNode* slow = head;
ListNode* fast = head;
while (fast->next != nullptr && fast->next->next != nullptr) {
fast = fast->next->next;
slow = slow->next;
}
ListNode* right = reverseList(slow->next);
slow->next = nullptr;
bool result = compare(head, right);
slow->next = reverseList(right);
return result;
}
示例9: test
void test( int* arr, int size ) {
node* root = newList( arr, size );
print( root );
reverseList( &root );
print( root );
freeList( root );
}
示例10: reorderList
void reorderList(ListNode* head) {
if (head==NULL || head->next==NULL || head->next->next==NULL)
{
return ;
}
ListNode* l1=head;
ListNode* l2=getMid(head);
ListNode* reverseL2=reverseList(l2);
ListNode* newhead=new ListNode(-1);
ListNode* tail=newhead;
while (l1!=NULL && reverseL2!=NULL)
{
tail->next=l1;
l1=l1->next;
tail=tail->next;
tail->next=reverseL2;
tail=tail->next;
reverseL2=reverseL2->next;
}
if (l1!=NULL)
{
tail->next=l1;
}
if (reverseL2!=NULL)
{
tail->next=reverseL2;
}
head=newhead->next;
}
示例11: main
int main() {
struct node * head = NULL;
struct node * second = NULL;
struct node * third = NULL;
head = (struct node *) malloc(sizeof(struct node));
second = (struct node *) malloc(sizeof(struct node));
third = (struct node *) malloc(sizeof(struct node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printf("Normal list order:\n");
printList(head);
printf("\nReversed order:\n");
reverseList(&head);
printList(head);
return 0;
}
示例12: reverseList
ListNode* reverseList(ListNode* head) {
if (!head || !(head->next)) return head;
ListNode* tmp = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return tmp;
}
示例13: reverseList
ListNode* reverseList(ListNode *head, int k) {
ListNode *prev, *current, *next, *tail;
current = head, prev = NULL, tail = head;
int cnt = 0;
while (current != NULL) {
if (cnt > k)
break;
current = current->next;
cnt++;
}
if (cnt < k)
return head;
cnt = 0;
current = head;
while (cnt < k && current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
cnt++;
}
if (current != NULL)
tail->next = reverseList(current, k);
return prev;
}
示例14: main
int main()
{
node *listHead = create();
if (!listHead)
return 0;
for (int i = 0; i < 10; i++)
add(listHead, i);
deleteNode(listHead, 5);
insertNode(listHead, 5, 50);
printf("Length = %d\n", length(listHead));
printList(listHead);
reverseList(listHead);
printList(listHead);
node *pMid;
pMid = getMid(listHead);
printf("Mid data is %d\n", pMid->data);
add(listHead, 99);
printList(listHead);
pMid = getMid(listHead);
printf("Mid data is %d\n", pMid->data);
destroy(listHead);
return 0;
}
示例15: reverseList
ListNode* reverseList(ListNode* head){
if(!head || !head->next) return head;
ListNode* newHead = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return newHead ;
}