LeetCode刷题2(链表OJ题)

目录

1. 反转链表

2.移除链表元素

3.链表的中间结点

4.合并两个排序的链表

5.分割链表

6.回文链表


1. 反转链表

 

typedef struct ListNode Node;
struct ListNode* reverseList(struct ListNode* head)
{
    Node* newnode = NULL;
    Node* cur = head;
    while(cur)
    {
        Node* next = cur->next;

        cur->next = newnode;
        newnode = cur;
        cur = next;
    }
    
    return newnode;
}

2.移除链表元素

 

typedef struct ListNode Node;
struct ListNode* removeElements(struct ListNode* head, int val)
{
    if(head == NULL)
        return NULL;
    
    //处理头结点为要删除的结点的情况
    while(head)
    {
        if(head->val == val)
            head = head->next;
        else
            break;
    }

    Node* prev = NULL;
    Node* cur = head;

    while(cur)
    {
        Node* next = cur->next;

        if(cur->val == val)
        {
            Node* temporary = cur;
            prev->next = next;
            free(temporary);
        }
        else
        {
            prev = cur;
        }

        cur = next;
    }

    return head;
}

3.链表的中间结点

该题使用的方法非常巧妙,前面我们提到,当没有好的思路时,可以试着往双指针的方向思考,本题就是用双指针来解决。

 

typedef struct ListNode Node;
struct ListNode* middleNode(struct ListNode* head)
{
    Node* slow = head;
    Node* fast = head;

    while(fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
    }

    return slow;
}

4.合并两个排序的链表

 

typedef struct ListNode Node;
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
{
    if(l1 == NULL)
        return l2;
    if(l2 == NULL)
        return l1;
    
    Node* sentryNode = (Node*)malloc(sizeof(Node));//哨兵位的头结点
    sentryNode->next = NULL;

    Node* cur1 = l1;
    Node* cur2 = l2;
    Node* tail = sentryNode;

    while(cur1 && cur2)
    {
        if(cur1->val <= cur2->val)
        {
            tail->next = cur1;
            cur1 = cur1->next;
        }
        else
        {
            tail->next = cur2;
            cur2 = cur2->next;
        }

        tail = tail->next;
    }

    if(cur1 == NULL)
    {
        while(cur2)
        {
            tail->next = cur2;
            tail = tail->next;
            cur2 = cur2->next;
        }
    }
    else
    {
        while(cur1)
        {
            tail->next = cur1;
            tail = tail->next;
            cur1 = cur1->next;
        }
    }
    
    Node* node = sentryNode->next;
    free(sentryNode);//malloc出来的空间记得释放

    return node;
}

5.分割链表

 

typedef struct ListNode Node;
struct ListNode* partition(struct ListNode* head, int x)
{
    if(head == NULL)
        return NULL;
    
    Node* cur = head;
    Node* sentryNode1 = (Node*)malloc(sizeof(Node));
    Node* sentryNode2 = (Node*)malloc(sizeof(Node));
    sentryNode1->next = sentryNode2->next = NULL;
    Node* tail1 = sentryNode1;
    Node* tail2 = sentryNode2;

    while(cur)
    {
        if(cur->val < x)
        {
            tail1->next = cur;
            tail1 = tail1->next;
        }
        else
        {
            tail2->next = cur;
            tail2 = tail2->next;
        }

        cur = cur->next;
    }

    tail1->next = sentryNode2->next;
    tail2->next = NULL;

    Node* newhead = sentryNode1->next;
    free(sentryNode1);
    free(sentryNode2);

    return newhead;
}

6.回文链表

 

typedef struct ListNode Node;
Node* reverse(Node* head)
{
    Node* newhead = NULL;
    Node* cur = head;
    while(cur)
    {
        Node* next = cur->next;
        cur->next = newhead;
        newhead = cur;
        cur = next;
    }

    return newhead;
}

bool isPalindrome(struct ListNode* head)
{
    if(head == NULL || head->next == NULL)
        return true;
    
    Node* prev = NULL;
    Node* slow = head;
    Node* fast = head;
    while(fast && fast->next)
    {
        prev = slow;
        slow = slow->next;
        fast = fast->next->next;
    }

    prev->next = NULL;
    slow = reverse(slow);

    while(head)
    {
        if(head->val != slow->val)
            return false;
        else
        {
            head = head->next;
            slow = slow->next;
        }
    }

    return true;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值