void merge_sort_list(ListNode *&head) { ListNode *sp = head, *fp = head->next; if (fp == NULL) return ; while(sp->next != NULL && fp->next != NULL && fp->next->next != NULL) { sp = sp->next; fp = fp->next->next; } fp = sp->next; sp->next = NULL; merge_sort_list(head); merge_sort_list(fp); ListNode newhead(0); sp = &newhead; while(head != NULL && fp != NULL) { if (head != NULL && fp != NULL) { if (head->val < fp->val) { sp->next = head; head = head->next; } else { sp->next = fp; fp = fp->next; } sp = sp->next; continue; } } if (head == NULL) { sp->next = fp; } else { sp->next = head; } head = newhead.next; }
ListNode* reverseKGroup(ListNode* head, int k) { ListNode newhead(0), *p = &newhead; newhead.next = head; while (p) { ListNode *kth = p, *newp = p->next; for (int i = 0; i < k; ++i) if (kth) kth = kth->next; if (!kth) break; ListNode *p1 = p->next, *p2 = p1->next; p1->next = kth->next; for (int i = 1; i < k; ++i) { ListNode *tp = p2->next; p2->next = p1; p1 = p2; p2 = tp; } p->next = kth; p = newp; } return newhead.next; }
RandomListNode *copyRandomList(RandomListNode *head) { if (head == NULL) return NULL; RandomListNode *p, *tmp, newhead(0), *tail; for (p = head; p!=NULL;){ tmp = p->next; p->next = new RandomListNode(p->label); p->next->next = tmp; p = tmp; } for (p = head; p!=NULL; p = p->next->next){ if (p->random){ p->next->random = p->random->next; } } tail = &newhead; for (p = head; p!=NULL; ){ tmp = p->next->next; tail = tail->next = p->next; p->next = tmp; p = tmp; } tail->next = NULL; return newhead.next; }
void Init () { pwr_tUInt32 sts; pwr_tInt32 ret; char msg[80]; pwr_tObjid MHObjId; pwr_sAttrRef AttrRef; pwr_tDlid DLId; pwr_sClass_MessageHandler *MH; char fname[200]; dcli_translate_filename( fname, DATABASE); dcli_translate_filename( info_fname, DATABASE_INFO); sts = gdh_GetNodeIndex(&lHelCB.Nid); If_Error_Log_Exit(sts, "gdh_GetNodeIndex"); sts = gdh_GetClassList(pwr_cClass_MessageHandler, &MHObjId); If_Error_Log_Exit(sts, "Couldn't find message handler object"); AttrRef.Objid = MHObjId; AttrRef.Body = 0; AttrRef.Offset = 0; AttrRef.Size = sizeof(pwr_sClass_MessageHandler); AttrRef.Flags.m = 0; sts = gdh_DLRefObjectInfoAttrref(&AttrRef, (pwr_tAddress *)&MH, &DLId); If_Error_Log_Exit(sts,"Couldn't get direct link to message handler object"); if (MH->EventLogSize == 0) { Log("EventLogSize = 0, no event logger will run on this node."); errh_SetStatus( pwr_cNStatus); exit(1); } lHelCB.MaxCardinality = MH->EventLogSize; lHelCB.MaxStoreLSize = 1000; /*not used*/ lHelCB.ScanTime = 2000; /* 5 seconds */ /*create the database if it's not already created*/ if((ret = db_create(&dataBaseP, NULL, 0)) != 0) { /*error creating db-handle send the mess to errh, then exit*/ sprintf(msg, "db_create: %s, no eventlogger will run", db_strerror(ret)); errh_Error(msg); exit(1); } /*open the database*/ #if (DB_VERSION_MAJOR > 3) && (DB_VERSION_MINOR > 0) ret = dataBaseP->open(dataBaseP, NULL, fname, NULL, DATABASETYPE, DB_CREATE, 0664); #else ret = dataBaseP->open(dataBaseP, fname, NULL, DATABASETYPE, DB_CREATE, 0664); #endif if(ret != 0) { /*error opening/creating db send the mess to errh, then exit*/ sprintf(msg, "db_open: %s, no eventlogger will run", db_strerror(ret)); Log(msg); exit(1); } newhead(&listhead); }