链表oj练习

例1

运用前插思想

c 复制代码
struct ListNode* reverseList(struct ListNode* head) {
    struct ListNode*pre=NULL;
    struct ListNode*cur=head;
    struct ListNode*tmp=NULL;
    while(cur)
    {
        tmp=cur->next;
        head=cur;
        cur->next=pre;
        pre=cur;
        cur=tmp;
    }
    return head;
}

例2

c 复制代码
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
 {
        struct ListNode* cur1=list1;
        struct ListNode* cur2=list2;
        struct ListNode* new=NULL;
        struct ListNode* newhead=NULL;
        while(cur1&&cur2)
        {
            if(cur1->val<=cur2->val)
            {
                if(newhead==NULL)
                {
                    newhead=cur1;
                    new=cur1;
                }
                else{
                    new->next=cur1;
                    new=cur1;
                }
                cur1=cur1->next;
            }
            else{
                if(newhead==NULL)
                {
                    newhead=cur2;
                    new=cur2;
                }
                else{
                    new->next=cur2;
                    new=cur2;
                }
                cur2=cur2->next;
            }
        }
        if(cur1==0)
            {
                while(cur2)
                {
                    if(newhead==NULL)
                {
                    newhead=cur2;
                    new=cur2;
                }
                else{
                    new->next=cur2;
                    new=cur2;
                }
                cur2=cur2->next;
                }
                
            }
            else
            {
                while(cur1)
                {
                     if(newhead==NULL)
                {
                    newhead=cur1;
                    new=cur1;
                }
                else{
                    new->next=cur1;
                    new=cur1;
                }
                cur1=cur1->next;
                }
                
                
            }
        return newhead;
 }

例子3

c 复制代码
#include<stdio.h>
#include<stdlib.h>
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        ListNode* lit=NULL;
        ListNode* big=NULL;
        ListNode* cur1=NULL;
        ListNode* cur2=NULL;
        ListNode* cur=pHead;
        while(cur)
        {
            if(cur->val<x)
            {
                if(lit==NULL)
                {
                    lit=cur;
                }
                else
                {
                    cur1->next=cur;
                }
                cur1=cur;
                cur=cur->next;
            }
            else
            {
                if(big==NULL)
                {
                    big=cur;
                }
                else
                {
                    cur2->next=cur;
                }
                cur2=cur;
                cur=cur->next;
            }
        }
        if(lit==NULL)
        return big;
        cur1->next=big;
        if(big==NULL)
        return lit;
        cur2->next=NULL;
        return lit;
    }
};

li4

c 复制代码
bool hasCycle(struct ListNode *head) {
 if(head==0||head->next==0)
    return 0;
 struct ListNode *slow=head;
 struct ListNode *fast=head->next;
 while(slow!=fast&&fast!=0&&fast->next!=0)
 {
    fast=fast->next->next;
    slow=slow->next;
 }      
 if(slow==fast)
 return 1;
 return 0;
}

尾插适合前哨兵

li5

复制代码
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{
    struct ListNode *curb=headB;
    struct ListNode *cura=headA;
    int cta=0;
    int ctb=0;
    while(cura)
    {
        cta++;
        cura=cura->next;
    }
    while(curb)
    {
        ctb++;
        curb=curb->next;
    }
    int len=abs(cta-ctb);
    if(cta>=ctb)
    {
        while(len--)
        {
            headA=headA->next;
        }
    }
    else{
        while(len--)
        headB=headB->next;
    }
    while(headB)
    {
        if(headA==headB)
        return headA;
        headA=headA->next;
        headB=headB->next;
    }
    return 0;
}

li6

复制代码
class PalindromeList {
public:
    bool chkPalindrome(ListNode* A) {
        ListNode*slow,*fast;
        slow=A;
        fast=A;
        while(fast&&fast->next)
        {
            fast=fast->next->next;
            slow=slow->next;
        }
        ListNode*newhead=NULL;
        ListNode*cur=slow;
        ListNode*pre=NULL;
        while(cur)
        {
            newhead=cur;
            cur=cur->next;
            newhead->next=pre;
            pre=newhead;
        }
        while(newhead)
        {
            if(A->val!=newhead->val)
            return 0;
            A=A->next;
            newhead=newhead->next;
        }
        return 1;

    }
};
相关推荐
散1124 小时前
01数据结构-图的概念和图的存储结构
数据结构
_poplar_7 小时前
09 【C++ 初阶】C/C++内存管理
c语言·开发语言·数据结构·c++·git·算法·stl
limitless_peter9 小时前
优先队列,链表优化
c++·算法·链表
屁股割了还要学11 小时前
【数据结构入门】栈和队列
c语言·开发语言·数据结构·学习·算法·青少年编程
啊阿狸不会拉杆16 小时前
《算法导论》第 13 章 - 红黑树
数据结构·c++·算法·排序算法
三次拒绝王俊凯16 小时前
用生活日常的案例来介绍“程序运行时,对函数的调用一般有两种形式:传值调用和引用调用 和 这两种调用有什么区别?
java·数据结构
码破苍穹ovo19 小时前
堆----3.数据流的中位数
java·数据结构·算法·力扣
武文斌771 天前
数据结构:哈希表、排序和查找
数据结构·散列表
Shun_Tianyou1 天前
Python Day25 进程与网络编程
开发语言·网络·数据结构·python·算法
Hx__2 天前
Redis中String数据结构为什么以长度44为embstr和raw实现的分界线?
数据结构·数据库·redis