链表的典型oj题

目录

1.反转一个单链表

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

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

4.将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的

5.输入一个链表,输出该链表中倒数第k个结点

6.链表的回文结构

7.输入两个链表,找出它们的第一个公共结点

8.给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。要求返回这个链表的深度拷贝

9.给定一个链表,判断链表中是否有环

10..给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL


1.反转一个单链表

206. 反转链表 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseList(struct ListNode* head) {
    if(head==NULL||head->next==NULL)
    {
        return head;
    }
    struct ListNode* n1=NULL;
    struct ListNode* n2=head;
    struct ListNode* n3=head->next;
    while(n2)
    {
        n2->next =n1;
        n1=n2;
        n2=n3;
        if(n3)
        n3=n3->next;
    }
    return n1;
}

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

203. 移除链表元素 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) {
        if(head==NULL)
        return head;
        struct ListNode*newhead=NULL,*ptail=NULL;
        struct ListNode*ptail1=head,*ptail2=head->next;
        while(ptail1)
        {
            if(ptail1->val!=val)
            {
                if(newhead==NULL)
                {
                    newhead=ptail=ptail1;
                }
                else
                {
                    ptail->next=ptail1;
                    ptail=ptail->next;
                }
               
            }
             ptail1=ptail2;
             if(ptail2)
             ptail2=ptail2->next;
        }
        if(ptail)
        ptail->next=NULL;
        return newhead;
}

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

876. 链表的中间结点 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* middleNode(struct ListNode* head) {
    struct ListNode* n1=head;
    struct ListNode* n2=head->next;
    while(n2)
    {
        n1=n1->next;
        n2=n2->next;
        if(n2)
        n2=n2->next;
    }
    return n1;
}

4.将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的

21. 合并两个有序链表 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    if(list1==NULL&&list2==NULL)
    return NULL;

    struct ListNode*newhead=(struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode*ptail=newhead;
    struct ListNode*ptail1=list1,*ptail2=list2;
    while(ptail1&&ptail2)
    {
        if(ptail1->val<=ptail2->val)
        {
            ptail->next=ptail1;
            ptail1=ptail1->next;
            ptail=ptail->next;
        }
        else
        { 
            ptail->next=ptail2;
            ptail2=ptail2->next;
            ptail=ptail->next;
        }

    }
    if(ptail1)
    ptail->next=ptail1;
    if(ptail2)
    ptail->next=ptail2;
    return newhead->next;

}

5.输入一个链表,输出该链表中倒数第k个结点

面试题 02.02. 返回倒数第 k 个节点 - 力扣(LeetCode)

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


int kthToLast(struct ListNode* head, int k){
    struct ListNode* ptail =head;
    struct ListNode* prev =head;
    while(k--)
    {
        ptail=ptail->next;
    }
    while(ptail)
    {
        prev=prev->next;
        ptail=ptail->next;
    }
    return prev->val;
}

6.链表的回文结构

链表的回文结构_牛客题霸_牛客网 (nowcoder.com)

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
  public:
    struct ListNode* middleNode(struct ListNode* head)
    {
        struct ListNode* n1 = head;
        struct ListNode* n2 = head->next;
        while (n2) {
            n1 = n1->next;
            n2 = n2->next;
            if (n2)
                n2 = n2->next;
        }
        return n1;
    }
    struct ListNode* reverseList(struct ListNode* head)
    {
        if (head == NULL || head->next == NULL) {
            return head;
        }
        struct ListNode* n1 = NULL;
        struct ListNode* n2 = head;
        struct ListNode* n3 = head->next;
        while (n2) {
            n2->next = n1;
            n1 = n2;
            n2 = n3;
            if (n3)
                n3 = n3->next;
        }
        return n1;
    }
 
 
    bool chkPalindrome(ListNode* A) {
 
        struct ListNode* midnode = middleNode(A);
         struct ListNode*newhead = reverseList(midnode);
          struct ListNode* ptail1=A,*ptail2=newhead;
         while(ptail2)
         {
            if(ptail1->val!=ptail2->val)
            {
                return false;
            }
            ptail1=ptail1->next;
            ptail2=ptail2->next;
         }
            return true;
 
 
    }
};

7.输入两个链表,找出它们的第一个公共结点

160. 相交链表 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    if(headA==NULL||headB==NULL)
    {
        return NULL;
    }
    struct ListNode *ptail1 =headA;
    struct ListNode *ptail2 =headB;
    int count1=0,count2=0;
    while(ptail1)
    {
        count1++;
        ptail1=ptail1->next;
    }
    while(ptail2)
    {
        count2++;
        ptail2=ptail2->next;
    }
    ptail1=headA;
    ptail2=headB;

    int max=count1;
    int min=count2;
    int tmp=0;
    int flag=1;
    if(max<count2)
    {
       tmp=max;
       max=min;
       min=tmp;
       flag=0;
    }

    int count3 =max-min;
    //ptail1弄多的
    if(flag==0)
    {
        struct ListNode * tmp =ptail1;
        ptail1=ptail2;
        ptail2=tmp;
    }  


    while(count3--)
    {
        ptail1=ptail1->next;
    }
     
    while(ptail1)
    {
        if(ptail1==ptail2)
        {
            return ptail1;
        }
        ptail1=ptail1->next;
        ptail2=ptail2->next;
    }
    return NULL;
}

8.给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。要求返回这个链表的深度拷贝

138. 随机链表的复制 - 力扣(LeetCode)

/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     struct Node *next;
 *     struct Node *random;
 * };
 */

struct Node* copyRandomList(struct Node* head) {
    if(head==NULL)
    return NULL;
	struct Node* cur =head;
     struct Node* copy=NULL;
     //拷贝每个节点并嵌入到两个原节点中间
    while(cur)
    {
        copy =(struct Node*)malloc(sizeof(struct Node));
        copy->val=cur->val;
        copy->next=cur->next;
        cur->next=copy;
        cur=copy->next;
    }

    cur=head;
   

    //处理random指针
    while(cur)
    {
        copy=cur->next;
        if(cur->random)
        copy->random=cur->random->next;
        else
        copy->random=NULL;
        cur=copy->next;
    }


    //穿起copy节点
    cur=head->next;
    struct Node* newhead=(struct Node*)malloc(sizeof(struct Node));
    struct Node* ptail=newhead;
    while(cur)
    {
        ptail->next=cur;
        ptail=ptail->next;
        if(cur->next)
        cur=cur->next->next;
        else
        cur=NULL;
    }
    return newhead->next;


}

9.给定一个链表,判断链表中是否有环

141. 环形链表 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head) {
    if(head==NULL||head->next==NULL)
    return false;
    struct ListNode *ptail1=head;
    struct ListNode *ptail2=head;
    while(ptail2)
    {
        ptail1=ptail1->next;
        if(ptail2)
        ptail2=ptail2->next;
        if(ptail2)
        ptail2=ptail2->next;
        if(ptail1==ptail2)
        return true;
    }
    return false;
}

10..给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL

142. 环形链表 II - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) {
    if(head==NULL||head->next==NULL)
    return NULL;
struct ListNode *ptail1=head;
struct ListNode *ptail2=head;
while(ptail2)
{
    ptail1=ptail1->next;
    if(ptail2)
    ptail2=ptail2->next;
    if(ptail2)
    ptail2=ptail2->next;
    if(ptail1==ptail2)
    break;
}
if(ptail2==NULL)
return NULL;

ptail1=head;
while(1)
{
    if(ptail1==ptail2)
    {
        return ptail1;
    }
    ptail1=ptail1->next;
    ptail2=ptail2->next;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值