Exemplo n.º 1
0
 ListNode* reverseBetween(ListNode* head, int m, int n) {
     if(head == nullptr )
         return head;
         
     ListNode dummyHead(0);
     ListNode* pre = &dummyHead;
     pre->next = head;
     
     for(int i = 1; i < m && pre->next != nullptr; ++i)
          pre = pre->next;
          
     ListNode* cur = pre->next;
     ListNode* tail = cur;
     ListNode* beg = pre;
     
     for(int i = m; i <=n && cur != nullptr; ++i)
     {
         ListNode* pNext = cur->next;
         cur->next = pre;
         pre = cur;
         cur = pNext;
     }
     
     beg->next = pre;
     tail->next = cur;
     return dummyHead.next;
 }
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
ListNode* Solution::partition(ListNode* head, int B) {
    // Do not write main() function.
    // Do not read input, instead use the arguments to the function.
    // Do not print the output, instead return values as specified
    // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
    ListNode dummy(-1);
    dummy.next = head;          // original list
    
    ListNode dummyHead(INT_MIN);
    ListNode *p = &dummyHead;  // candidate list
    
    ListNode* prev = &dummy;
    while(head != NULL)
    {
        ListNode *next = head->next;
        if(head->val < B)
        {
            // append to the candidate list
            p->next = head;
            p = p->next;
            // remove from the original list
            prev->next = next;
            head = next;
        }
        else
        {
            prev = head;
            head = next;
        }
    }
    p->next = dummy.next;
    
    return dummyHead.next;
}
ListNode *reverseKGroup(ListNode *head, int k) {
    if (head == nullptr || k <= 1) return head;

    ListNode dummyHead(0);
    dummyHead.next = head;
    ListNode *beginK = head;
    ListNode *endK = head;
    ListNode *prevNode = &dummyHead;
    for (;;)
    {
        int steps = k;
        while (endK != nullptr && steps-- > 0)
            endK = endK->next;
        if (steps > 0) break;

        ListNode *pPrev = endK;
        ListNode *pCurr = beginK;
        do {
            ListNode *pDummy = pCurr->next;
            pCurr->next = pPrev;
            pPrev = pCurr;
            pCurr = pDummy;
        } while (pCurr != endK);

        beginK->next = endK;
        prevNode->next = pPrev;
        prevNode = beginK;
        beginK = endK;
    }

    return dummyHead.next;
}
    ListNode* reverseList(ListNode* head) {
       if(!head)return NULL;
       ListNode dummyHead(0); 
       dummyHead.next = head;
       ListNode *tail = &dummyHead;
       ListNode *front = head;

       while(front->next){
        ListNode *tmp = front->next;
        front->next = tmp->next;
        tmp->next = tail->next;
        tail->next = tmp;
       }
       return dummyHead.next;
    }
Exemplo n.º 5
0
 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
   ListNode dummyHead(0);
   ListNode *head = &dummyHead, *first = l1, *second = l2;
   while (first && second) {
     if (first->val <= second->val) {
       head->next = first;
       first = first->next;
     } else {
       head->next = second;
       second = second->next;
     }
     head = head->next;
   }
   head->next = first ? first : second;
   return dummyHead.next;
 }
Exemplo n.º 6
0
 ListNode* removeElements(ListNode* head, int val) {
     ListNode dummyHead(0);
     dummyHead.next = head;//虚拟头节点,可以将删除head节点转换成一般情况,无需特殊处理
     ListNode *previous = &dummyHead;//前趋
     ListNode *current = head;
     while(current) {
         if(current->val == val) {
             deleteNode(previous, current);//删除current节点
             current = previous->next;
         }
         else {
             previous = current;
             current = current->next;
         }
     }
     return dummyHead.next;//返回真正的head
 }
 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
     ListNode dummyHead(0);
     ListNode *tail = &dummyHead;
     int c=0;
     while(l1 || l2){
       int tmpValue = (l1?l1->val:0) + (l2?l2->val:0) + c;
       c = tmpValue/10;
       tmpValue%=10;
       ListNode *newNode = new ListNode(tmpValue);
       tail = tail->next = newNode;
       if(l1)l1=l1->next;
       if(l2)l2=l2->next;
     }
     if(c){
       ListNode *newNode = new ListNode(c);
       tail = tail->next = newNode;
     }
     return dummyHead.next;
 }
Exemplo n.º 8
0
 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
     if(l1 == nullptr) return l2;
     if(l2 == nullptr) return l1;
     
     ListNode dummyHead(0);
     ListNode* pre = &dummyHead;
     int carry = 0;
     
     while(l1 != nullptr && l2 != nullptr)
     {
         int sum = l1->val + l2->val + carry;
         carry = sum / 10;
         pre->next = new ListNode(sum % 10);
         l1 = l1->next;
         l2 = l2->next;
         pre = pre->next;
     }
     
     while(l1 != nullptr)
     {
         int sum = l1->val + carry;
         carry = sum / 10;
         pre->next = new ListNode(sum % 10);
         l1 = l1->next;
         pre = pre->next;
     }
     
     while(l2 != nullptr)
     {
         int sum = l2->val + carry;
         carry = sum / 10;
         pre->next = new ListNode(sum % 10);
         l2 = l2->next;
         pre = pre->next;
     }
     if(carry == 1)
         pre->next = new ListNode(carry);
         
     return dummyHead.next;
 }
Exemplo n.º 9
0
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if(head == nullptr || m <= 0 || n <= 0 || m >= n)
            return head;

        ListNode dummyHead(0);
        ListNode* pPre = &dummyHead;
        pPre->next = head;
        ListNode* pEnd = head;
        
        for(int i = 1; i < m && pPre != nullptr; ++i)
        {
            pPre = pPre->next;
            pEnd = pEnd->next;
        }
            
        ListNode* pBeg = pPre;
        if(pBeg->next != nullptr) pBeg = pBeg->next;
        else return head;
        
        for(int i = m; i <= n && pEnd != nullptr; ++i)
            pEnd = pEnd->next;
            
        auto reverseList = [](ListNode* pBeg, ListNode* pEnd)
        {
            ListNode* pPre = pBeg;
            pBeg = pBeg->next;
            while(pBeg != pEnd)
            {
                ListNode* pNext = pBeg->next;
                pBeg->next = pPre;
                pPre = pBeg;
                pBeg = pNext;
            }
            return pPre;
        };
        pPre->next = reverseList(pBeg, pEnd);
        pBeg->next = pEnd;
        
        return dummyHead.next;
    }
        ListNode *deleteDuplicates(ListNode *head) {
            if(head == NULL) return NULL;
            if(head->next == NULL) return head;
            ListNode dummyHead(0); dummyHead.next = NULL;
            ListNode *p1 = head, *p2 = head->next, *tail = NULL;

            while(p1 != NULL){
                int distance = 0;
                while(p2 != NULL && p1->val == p2->val){
                    p2 = p2->next; ++ distance;
                }
                if(distance == 0){
                    if(dummyHead.next == NULL){
                        dummyHead.next = p1; tail = p1; tail->next = NULL;
                    }else{
                        tail->next = p1; tail = tail->next; tail->next = NULL;
                    }
                    p1 = p2; if(p2 != NULL) p2 = p2->next;
                }else{
                    p1 = p2; if(p2 != NULL) p2 = p2->next;
                }
            }
            return dummyHead.next;        
        }
Exemplo n.º 11
0
    ListNode* reverseBetween(ListNode* head, int m, int n)
    {
        if (m == n)
        {
            return head;
        }

        ListNode dummyHead(-1);
        dummyHead.next = head;
        ListNode *curNode = &dummyHead;
        int count = 1;
        while (count < m)
        {
            curNode = curNode->next;
            count++;
        }

        ListNode tempHead(-1);
        for (int curInd = m; curInd <= n; curInd++)
        {
            ListNode *tempNode = curNode->next;
            curNode->next = curNode->next->next;
            tempNode->next = tempHead.next;
            tempHead.next = tempNode;
        }

        ListNode *curTemp = tempHead.next;
        while (curTemp->next != NULL)
        {
            curTemp = curTemp->next;
        }
        curTemp->next = curNode->next;
        curNode->next = tempHead.next;

        return dummyHead.next;
    }