1. 程式人生 > >資料結構面試題oj練習

資料結構面試題oj練習

      

oj 連結:https://leetcode-cn.com/problems/remove-linked-list-elements/description/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) {
    
    struct ListNode* pCur = head;
    struct ListNode* pPre = NULL;
    
    while (pCur)
    {
        if (val == pCur->val)
        {
            if(head == pCur)
            {
                head = pCur->next;
                free(pCur);
                pCur = head;
            }
            else
            {
                pPre->next = pCur->next;
                free(pCur);
                pCur = pPre->next;
            }
        }
        else
        {
            pPre = pCur;
            pCur = pCur->next;
        }
    }
    return head;
}

總結: 做這道題,一定要注意返回值是什麼!!!!!!!

 

 

方法一:三個指標處理:https://leetcode-cn.com/problems/reverse-linked-list/description/

struct ListNode* reverseList(struct ListNode* head) {
    
    struct ListNode* pCur = head;
    struct ListNode* pPre = NULL;
    struct ListNode* pNext = NULL;
    
    while (pCur)
    {
        pNext = pCur->next;
        pCur->next = pPre;
        pPre = pCur;
        pCur = pNext;
    }
    return pPre;
}

利用三個指標處理,其核心是pCur起指向作用,pPre和pNext僅僅作為pCur的前後結點指標使用!!!

 

方法二:頭插的思想,重新弄一個連結串列頭指標,依次將老連結串列的結點從後往前插入即可!!!

https://leetcode-cn.com/problems/reverse-linked-list/description/

 

struct ListNode* reverseList(struct ListNode* head) {
    
    struct ListNode* pnewHead = NULL;
    struct ListNode* pCur = head;
    
    while (pCur)
    {
        head = head->next;
        pCur->next = pnewHead;
        pnewHead = pCur;
        pCur = head;
    }
    return pnewHead;
}

https://leetcode-cn.com/problems/middle-of-the-linked-list/

struct ListNode* middleNode(struct ListNode* head) {
    struct ListNode* cur = head;
    struct ListNode* pre = head;
    int size = 0;
    while(cur)
    {
        size++;
        cur = cur->next;
    }
    size = size/2;
    while(size)
    {
        pre = pre->next;
        size--;
    }
    return pre;
}

方法二:但是條件如果改為只能遍歷一次,該怎麼辦?

https://leetcode-cn.com/problems/middle-of-the-linked-list/submissions/

struct ListNode* middleNode(struct ListNode* head) {
    if(head == NULL)
        return NULL;
    
    struct ListNode* slow = head;
    struct ListNode* fast = head;
    while(fast != NULL && fast->next != NULL)
    {  
        slow = slow->next;
        fast = fast->next->next;    
    }
    return slow; 
}

 

https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=13&tqId=11167&rp=2&ru=%2Factivity%2Foj&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking&tPage=1

class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
        if(NULL == pListHead)
            return NULL;
        
        ListNode* pSlow = pListHead;
        ListNode* pFast = pListHead;
        
        while(k--)
        {
            if(NULL == pFast)
                return NULL;
            pFast = pFast->next;
        }
        while(pFast)
        {
            pSlow = pSlow->next;
            pFast = pFast->next;
        }
        return pSlow;
    }
};