Esempio n. 1
0
 ListNode* deleteDuplicates(ListNode* head) {
     ListNode dummy = ListNode(-1);
     dummy.next = head;
     ListNode *pre = &dummy, *cur = head, *first = head;
     while (cur)
     {
         if (cur->val != first->val)
         {
             if (first->next == cur)
             {
                 pre = first;
                 first = cur;
             }
             else
             {
                 deleteBetween(pre, cur);
                 first = cur;
             }
         }
         cur = cur->next;
     }
     if (first && first->next)
         deleteBetween(pre, nullptr);
     return dummy.next;
 }
Esempio n. 2
0
 /**
  * @param head: The head of linked list.
  * @param m: The start position need to reverse.
  * @param n: The end position need to reverse.
  * @return: The new head of partial reversed linked list.
  */
 ListNode *reverseBetween(ListNode *head, int m, int n) {
     // write your code here
     ListNode dummy = ListNode(-1);
     dummy.next = head;
     head = &dummy;
     for (int i = 0; i < m - 1; ++i)
     {
         head = head->next;
     }
     ListNode* prev = head;
     ListNode* curr = prev->next;
     ListNode* nodem = curr;
     for (int i = m; i <= n; ++i)
     {
         ListNode* next = curr->next;
         curr->next = prev;
         prev = curr;
         curr = next;
     }
     
     head->next = prev;
     nodem->next = curr;
     
     return dummy.next;
 }
Esempio n. 3
0
int main(void)
{
    int n;
    NODE *pHead = Init();
    NODE *pHead2 = Init();
    NODE *tar = pHead2;
   // scanf("%d", &n);
    Creat(pHead, 5);
    tar = ListNode(pHead, 0);
    if(tar != NULL)
        printf("%d\n", tar->data);
    else
        printf("有误!\n");
}
    ListNode* reverseKGroup(ListNode* head, int k) {
        if (k < 2)
        {
            return head;
        }
        
        ListNode newHead = ListNode(0);
    
        int count = 0;
        
        ListNode *segHead = &newHead;
        ListNode *cur = head;
        
        // reverse very k nodes
        while (cur)
        {
            ListNode *tmp = cur;
            cur = cur->next;
            
            tmp->next = segHead->next;
            segHead->next = tmp;
            count ++;
                
            if (count % k == 0)
            {
                while(segHead->next)
                {
                    segHead = segHead->next;
                }
            }
        }
        
        // reverse back last < k
        if (count % k != 0)
        {
            cur = segHead->next;
            segHead->next = NULL;
            while (cur)
            {
                ListNode *tmp = cur;
                cur = cur->next;

                tmp->next = segHead->next;
                segHead->next = tmp;
            }
        }
        
        return newHead.next;
    }
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head == NULL) return head;
		if(head->next == NULL && n == 1) return head->next;
		ListNode dummy = ListNode(-1);
		dummy.next = head;
		int len = length(head);
		ListNode *del = index(head,len - n);
		ListNode *pre = &dummy;
		while(pre->next != del) {
			pre = pre->next;
		}
		pre->next = pre->next->next;
		delete del;
		return dummy.next;
    }
Esempio n. 6
0
 ListNode* mergeList(ListNode* head1,ListNode* head2){
     ListNode dummy = ListNode(-1);
     ListNode* walk = &dummy;
     while(head1&&head2){
             walk->next = head1;
             head1 = head1->next;
             walk = walk->next;
             walk->next = head2;
             head2 = head2->next;
             walk = walk->next;
     }
     if(head1) walk->next =head1;
     if(head2) walk->next = head2;
     return dummy.next;
 }
	ListNode* deleteDuplicates(ListNode* head) {
		if(head == NULL || head->next == NULL) return head;
		ListNode dummy = ListNode(-1);
		dummy.next = head;
		ListNode *pre = &dummy;
		ListNode *_next = NULL;
		while(pre->next && pre->next->next) {
			if(pre->next->val == pre->next->next->val) {
				_next = next(pre->next, pre->next->val);
				pre->next = _next;
			} else {
				pre = pre->next;
			}		
		}
		return dummy.next;
	}
Esempio n. 8
0
 ListNode* mergeKLists(vector<ListNode*>& lists) {
     priority_queue<ListNode*, vector<ListNode*>, cmp> q;
     for(auto head : lists){
         while(head){
             q.push(head);
             head = head->next;
         }
     }
     ListNode aux = ListNode(0), *head = &aux;
     while(!q.empty()){
         head->next = q.top();
         q.pop();
         head = head->next;
     }
     if(head) head->next = NULL;
     return aux.next;
 }
 ListNode *reverseBetween(ListNode *head, int m, int n) {
     ListNode ans = ListNode(0);
     ans.next = head;
     ListNode *hm =  &ans, *hn, *tmp, *last;
     for (int i = 0; i < m - 1; i++)
         hm = hm->next;
     last = hm->next;
     hn = hm->next;
     hm->next= NULL;
     for (int i = m; i <= n; i++) {
         tmp = hn->next;
         hn->next = hm->next;
         hm->next = hn;
         hn = tmp;
     }
     last->next = hn;
     return ans.next;
 }
    ListNode *mergeKLists(vector<ListNode *> &lists) {
        priority_queue<ListNode*, vector<ListNode*>, cmp> q;
        ListNode ans = ListNode(0);
        ListNode* ahead = &ans;

        for (int i = 0; i < lists.size(); i++) {
            if (lists[i])
                q.push(lists[i]);
        }
        while (!q.empty()){
            ListNode* head = q.top();
            q.pop();
            ahead->next = head;
            if (head->next != NULL)
                q.push(head->next);
            ahead = ahead->next;
            ahead->next = NULL;
        }
        return ans.next;
    }
Esempio n. 11
0
		ListNode* mergeKLists(vector<ListNode*>& lists) {
			ListNode head = ListNode(0);
			ListNode* ans = &head;
			ListNode* move = ans;

			priority_queue<pair<int, int>> que;
			for (int i = 0; i < lists.size(); i++) {
				if (lists[i] != NULL)
					que.push(make_pair(-lists[i]->val, i));
			}

			while (!que.empty()) {
				int t = que.top().second; que.pop();
				move->next = lists[t];
				lists[t] = lists[t]->next;
				if (lists[t] != NULL)
					que.push(make_pair(-lists[t]->val, t));
				move = move->next;
			}

			return ans->next;
		}
Esempio n. 12
0
 ListNode* deleteDuplicates(ListNode* head) {
     ListNode dummy = ListNode(-1);
     dummy.next = head;
     ListNode *pre = &dummy, *cur = head, *next;
     int cmp;
     while (cur)
     {
         cmp = cur->val;
         next = cur->next;
         if (next && next->val == cmp)
         {
             while (next && next->val == cmp)
                 next = next->next;
             pre->next = next;
             cur = next;
         }
         else
         {
             pre = cur;
             cur = next;
         }
     }
     return dummy.next;
 }
Esempio n. 13
0
void stackMatching(bufferarray *block){                                     //每个缓存块内的栈匹配
    if(block == NULL)   return;
    bcs *r = block->bcsarr;
    stack *st = creatStack();
    char *name = (char*)malloc(NAMELEN * sizeof(char));
    strcpy(name, getTagName(name, &(block->buf[r->offset]), r->bt));
    lable* node = creatData(name, r->bt);
    Push(st, node);
    r = r->next;
    while(r->next != NULL){                                                 //该块最后一个标签之前的表签名分析
        stackInOut(block, &(block->buf[r->offset]), r->bt, st, r);
        r = r->next;
    }
    if(block->next != NULL){
        int i, j;
        j = block->next->bcsarr->offset;
        if(r->bt == Stag_start){                                                    //最后一个标签为开始标签,直接填充下一块到开始标签的部分
            //printf("该块最后一个字符是:%s\n",&(block->buf[block->buflen-1));
            strncat(block->buf,block->next->buf,j-1);   //j-1是刚好没问题的   j处为"<"
            //printf("该块最后一个字符+追加的不部分%s\n",&(block->buf[block->buflen-j]));
            block->buflen = block->buflen + j-1;
            //printf("该块最后10个字符是%s\n",&(block->buf[block->buflen-10]));
        }else{                                                                      //最后一个不是开始标签,判断
            if(block->buf[block->buflen-1] != '>'){
                //printf("前%s\n",&(block->buf[r->offset]));       //打印最后该块最后一个标签之后的内容
                strncat(block->buf,block->next->buf,j-1);   //j-1是刚好没问题的   j处为"<"
                //printf("后%s\n",&(block->buf[r->offset-1]));       //打印最后该块最后一个标签之后的内容
                block->buflen = block->buflen + j-1;
            }
        }
////////////////////////////////////////填充结束////////////

////////////////////////////////////////下面开始识别标签/////
        i = r->offset;
        j = block->next->bcsarr->offset;

        int k, flag = 0;
        while(i < block->buflen){                           //该块最后一个标签的识别
            if(block->buf[i++] != '<')    continue;
            if(block->buf[i]!='/'&&block->buf[i]!='?'&&block->buf[i]!='!'){             //对<xxxx/>情况的过滤
                flag = 0;
                k = i;
                while(k < block->buflen){
                    if(strCmp(&(block->buf[k++]), 0, 2, "/>")){
                        i = k+1;
                        flag = 1;
                        break;
                    }
                }
                if(flag == 1){
                    i++;
                    continue;
                }
            }
            if(block->buf[i]!='/'&&block->buf[i]!='?'&&block->buf[i]!='!'){             //<xxxx>标签的处理
                stackInOut(block, &(block->buf[i]), Stag_start, st, r);
                continue;
            }else if(block->buf[i]!='/'){                                               //非结束标签则:continue
                i++;
                continue;
            }else{                                                                      //</xxxx>标签的处理
                stackInOut(block, &(block->buf[i]), Etag_start, st, r);
            }
        }//  */
    }

    pthread_mutex_lock(&(block->mutex));
    block->START_STAGE2 = 0;                                                    //访问修改均加锁进行
    block->FINISH_STAGE2 = 1;
    pthread_mutex_unlock(&(block->mutex));

    if(st->top != -1){                                                          //栈非空将数据写入第三阶段的数组中
/*      //该部分为加互斥锁追加到第三阶段的链表部分
        list* tmp = addListNode(block->bufnum, Pop(st));
        pthread_mutex_lock(&mutex);
        point->next = tmp;
        point = point->next;
        pthread_mutex_unlock(&mutex);
        lab *labnode = point->data;
        while(st->top > -1){
            labnode->next = addLabNode(Pop(st));
            labnode = labnode->next;
        }// */

        //printf("块号%d\t栈非空!\t栈内元素个数%d\n", block->bufnum, st->top+1);//st->data[st->top]->tagname);
        lab* tmp = ListNode(block->bufnum, st->data[0]);
        int m = 1;
        while(m <= st->top){
            tmp = addLabNode(tmp,st->data[m++]);
        }
        while(st->top > -1){
            node = Pop(st);
            free(node);
        }
    }else{
        //printf("块号%d\t栈空!\n", block->bufnum);
    }
    free(st);//  */
}