链表小题(反转链表 求倒数第k个结点 删除链表指定结点)

目录

1. 删除链表中等于给定值 val 的所有节点。力扣

2. 反转一个单链表。力扣

3. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个 中间结点 力扣

4. 输入一个链表,输出该链表中倒数第k个结点。 链表中倒数第k个结点_牛客题霸_牛客网


1. 删除链表中等于给定值 val 的所有节点。力扣

2. 反转一个单链表。力扣

3. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个 中间结点 力扣

4. 输入一个链表,输出该链表中倒数第k个结点。链表中倒数第k个结点_牛客题霸_牛客网 

1、删除链表中等于给定值 val 的所有节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* removeElements(struct ListNode* head, int val){

 struct ListNode* prev=NULL,*cur=head;
  
    while(cur)
    {

        if(cur->val==val)
        {
            
struct ListNode* next=cur->next;
            if(prev==NULL)
            {
                free(cur);
               // prev=cur;
                cur=next;
                head=next;
            }
            else
            {
                 prev->next=next;
            free(cur);
            cur=next;
            }
        }
        else
        {
            prev=cur;
            cur=cur->next;
        }
    }
    return head;
}

 2、反转一个单链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* reverseList(struct ListNode* head){
    struct ListNode* newhead=NULL,*cur=head,*next=NULL,*next1=NULL;
    while(cur)
    {
        next=cur;
        next1=cur->next;
        cur->next=newhead;
        newhead=cur;
        cur=next1;

    }
    return next;

}

 3、给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个 中间结点 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* middleNode(struct ListNode* head){

struct ListNode* fast=head,*slow=head;
while(fast&&fast->next!=NULL)
{
   
    fast=fast->next->next;
    slow=slow->next;

    //  if(fast==NULL)
    // break;
    
}
return slow;


}

 4、输入一个链表,输出该链表中倒数第k个结点。

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead,unsigned int k) {
       ListNode* fast=pListHead,*slow=pListHead;
        //双指针法->
//         while(k--)
//         {
//             if(fast==NULL)
//                 return NULL;
//             fast=fast->next;
//         }
//         while(fast&&slow)
//         {
//             fast=fast->next;
//             slow=slow->next;
//         }
//     return slow;
        //循环法->
       int lenth=0;
        while(fast)
        {
            ++lenth;
            fast=fast->next;
        }
        if(lenth<k)
            return NULL;
        int m=lenth-k;
        while(m)
        {
            slow=slow->next;
            m--;
        }
        return slow;
        
    }
};

  • 14
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 21
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值