数据结构面试题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;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_42080151/article/details/84143030