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

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值