首页 > 其他分享 >[牛客]链表分割

[牛客]链表分割

时间:2023-04-20 22:42:35浏览次数:43  
标签:分割 greaterTail ListNode struct cur next 链表 牛客 LessTail

编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 。


牛客链接


[牛客]链表分割_死循环

最简单思路

因为头插必然改变顺序,所以使用尾插

大于的尾插到一个链表

小于的尾插到一个链表

然后链接起来

使用哨兵位的头结点,防止太多问题的产生


[牛客]链表分割_链表_02

/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
    // write code here
    struct ListNode* LessTail,* LessGuardHead,* greaterGuardHead,* greaterTail;     
    LessGuardHead = LessTail = (struct ListNode*)malloc(sizeof(struct ListNode));
    greaterGuardHead = greaterTail = (struct ListNode*)malloc(sizeof(struct ListNode));
    greaterTail->next = LessTail->next = NULL;

    struct ListNode* cur = pHead;
    while(cur)
        {
            if(cur->val < x)
            {
                LessTail->next = cur;
                LessTail = LessTail->next;
            }
            else 
            {
                greaterTail->next = cur;
                greaterTail = greaterTail->next;

            }
            cur = cur->next;
        }
    LessTail->next = greaterGuardHead->next;



    pHead = LessGuardHead->next;
    free(greaterGuardHead);
    free(LessGuardHead);
    return pHead;

}
};

[牛客]链表分割_死循环_03

内存超限一般是发生了死循环,而单链表中发生死循环,我们考虑环状链表,如下图两个链表组合后,最后一个结点还指向原来后面的结点,构成了环状链表,所以尾插后切记要置空!!


[牛客]链表分割_死循环_04

最终代码:

/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
    // write code here
    struct ListNode* LessTail,* LessGuardHead,* greaterGuardHead,* greaterTail;     
    LessGuardHead = LessTail = (struct ListNode*)malloc(sizeof(struct ListNode));
    greaterGuardHead = greaterTail = (struct ListNode*)malloc(sizeof(struct ListNode));
    greaterTail->next = LessTail->next = NULL;

    struct ListNode* cur = pHead;
    while(cur)
        {
            if(cur->val < x)
            {
                LessTail->next = cur;
                LessTail = LessTail->next;
            }
            else 
            {
                greaterTail->next = cur;
                greaterTail = greaterTail->next;

            }
            cur = cur->next;
        }
    LessTail->next = greaterGuardHead->next;

    greaterTail->next = NULL;//避免环状链表

    pHead = LessGuardHead->next;
    free(greaterGuardHead);
    free(LessGuardHead);
    return pHead;

}
};


标签:分割,greaterTail,ListNode,struct,cur,next,链表,牛客,LessTail
From: https://blog.51cto.com/u_15992140/6210795

相关文章

  • 单链表的实现
    链表的概念我们知道顺序表的储存方式是一片连续的空间里面储存数据而链表就不一样了,从这个图我们可以看到在一个链表里面有两个储存空间的部分,一部分是用以储存我们的数据,而另一部分储存的是一个结构体的地址,而这个地址指向的空间里面也有两个部分的储存空间用处和上面的一样,直到最......
  • 四种语言刷算法之对链表进行插入排序
    力扣147. 对链表进行插入排序1、C/***Definitionforsingly-linkedlist.*structListNode{*intval;*structListNode*next;*};*/structListNode*insertionSortList(structListNode*head){structListNode*newHead=head;struc......
  • LeetCode Top100:回文链表 (python)
    LeetCodeTop100:回文链表给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。示例1:输入:head=[1,2,2,1]输出:true示例2:输入:head=[1,2]输出:false提示:链表中节点数目在范围[1,105] 内0<=Node.val<=9 ......
  • LeetCode Top100: 相交链表(Python)
    LeetCodeTop100:相交链表给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。图示两个链表在节点 c1 开始相交:题目数据 保证 整个链式结构中不存在环。注意,函数返回结果后,链表必须 保持其原......
  • LeetCode Top100: 环形链表(python)
     给你一个链表的头节点 head ,判断链表中是否有环。如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从0开始)。注意:pos 不作为参数进行传递 。仅仅是为......
  • 【DP】LeetCode 132. 分割回文串 II
    题目链接132.分割回文串II思路分析动态规划题目的时候只需要考虑最后一个阶段,因为所有的阶段转化都是相同的,考虑最后一个阶段容易发现规律在数组的动态规划问题中,一般dp[i]都是表示以nums[i]为结尾的状态;dp[i][j]分别表示以nums1[i]和nums2[j]为结尾的状态,以此类......
  • [Leetcode]合并两个有序链表
    力扣链接依次比较,取小的尾插:初步代码:/***Definitionforsingly-linkedlist.*structListNode{*intval;*structListNode*next;*};*/structListNode*mergeTwoLists(structListNode*list1,structListNode*list2){structListNode*......
  • JZ18 删除链表的节点
    importjava.util.*;/**publicclassListNode{*intval;*ListNodenext=null;*publicListNode(intval){*this.val=val;*}*}*/publicclassSolution{/***代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法......
  • 力扣题解分享1043.分割数组以得到最大和
    1043.分隔数组以得到最大和题目描述给定一个整数数组arr和一个整数k,将该数组分隔为长度最多为k的一些连续子数组。分隔完成后,每个子数组中的所有值都会变为该子数组中的最大值。返回将数组分隔变换后能够得到的元素最大和。示例input:arr=[1,15,7,9,2,5,10]k=......
  • 牛客动态规划1选做
    [NOIP2002]过河卒题目链接进行记忆化搜索,然后强制把马所在的点和控制的点赋值为0#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;set<pair<int,int>>st;intf[25][25];intcalc(intx,inty){if(x==0&&y==0)returnf[x][y]=1......