链表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;

    }
};
相关推荐
天选之女wow40 分钟前
【代码随想录算法训练营——Day4】链表——24.两两交换链表中的节点、19.删除链表的倒数第N个节点、面试题02.07.链表相交、142.环形链表II
数据结构·算法·leetcode·链表
胡萝卜3.042 分钟前
数据结构初阶:树的相关性质总结
数据结构·二叉树·性质·二叉树的性质
KarrySmile1 小时前
Day12--HOT100--23. 合并 K 个升序链表,146. LRU 缓存,94. 二叉树的中序遍历
数据结构·链表·二叉树·递归·hot100·lru·灵茶山艾府
Miraitowa_cheems7 小时前
LeetCode算法日记 - Day 34: 二进制求和、字符串相乘
java·算法·leetcode·链表·职场和发展
今后1237 小时前
【数据结构】带哨兵位双向循环链表
数据结构·链表
Lee嘉图7 小时前
数据结构——队列(Java)
数据结构
梁辰兴8 小时前
数据结构:查找
数据结构·算法·查找·顺序查找·折半查找·分块查找
midsummer_woo9 小时前
#数据结构----2.1线性表
数据结构
ShineWinsu9 小时前
对于单链表相关经典算法题:206. 反转链表及876. 链表的中间结点的解析
java·c语言·数据结构·学习·算法·链表·力扣