c语言刷 链表题记录
61. 旋转链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/ struct ListNode* rotateRight(struct ListNode* head, int k)
{
if (head == NULL || head->next == NULL || k == 0) {
return head;
}
int len = 0;
struct ListNode* tail = head;
while (tail && tail->next) {
len++;
tail = tail->next;
}
len++; if (k % len == 0) {
return head;
} k = len - (k % len);
tail->next = head; while (k--) {
tail = tail->next;
head = head->next;
} tail->next = NULL;
return head;
}
148. 排序链表
暴力解法:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/ int Cmp(const void *a, const void *b)
{
return (*(int *)a) - (*(int *)b);
} struct ListNode* sortList(struct ListNode* head)
{
int *arr = (int *)malloc(sizeof(int) * 50000);
struct ListNode* tmp = head;
int len = 0;
while (tmp) {
arr[len++] = tmp->val;
tmp = tmp->next;
} qsort(arr, len, sizeof(int), Cmp); tmp = head;
for (int i = 0; i < len; i++) {
tmp->val = arr[i];
tmp = tmp->next;
} return head;
}
剑指 Offer 24. 反转链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/ struct ListNode* stack[5001];
int len; struct ListNode* reverseList(struct ListNode* head)
{
if (head == NULL) {
return NULL;
} len = 0;
struct ListNode* node = head;
struct ListNode* res;
// 入栈
while (node) {
stack[len++] = node;
node = node->next;
} // 弹栈
len--;
res = stack[len--];
node = res;
while (len >= 0) {
node->next = stack[len--];
node = node->next;
}
node->next = NULL;
return res;
}
2. 两数相加
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/ struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode* head = NULL;
struct ListNode* tail = NULL;
int sum = 0;
int carry = 0; while (l1 != NULL || l2 != NULL) {
int l1Val = l1 ? l1->val : 0;
int l2Val = l2 ? l2->val : 0;
sum = l1Val + l2Val + carry;
printf("sum(%d) = l1Val(%d) + l2Val(%d) + carry(%d)\n", sum, l1Val, l2Val, carry); if (head == NULL) {
head = malloc(sizeof(struct ListNode));
tail = head;
head->val = sum % 10;
head->next = NULL;
} else {
tail->next = malloc(sizeof(struct ListNode));
tail->next->val = sum % 10;
tail = tail->next;
tail->next = NULL;
} carry = sum / 10; if (l1 != NULL) {
l1 = l1->next;
}
if (l2 != NULL) {
l2 = l2->next;
}
} if (carry > 0) {
printf("%d\n", carry);
tail->next = malloc(sizeof(struct ListNode));
tail->next->val = carry;
tail = tail->next;
tail->next = NULL;
} return head;
}
c语言刷 链表题记录的更多相关文章
- C语言刷数组题记录
讲解:https://mp.weixin.qq.com/s/weyitJcVHBgFtSc19cbPdw 二分法: 704. 二分查找 int search(int* nums, int numsSi ...
- c语言刷 队列题记录
622. 设计循环队列 https://blog.csdn.net/Galaxy_n/article/details/115978544 typedef struct { int *arrs; int ...
- c语言刷 DFS题记录
144. 二叉树的前序遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeN ...
- 用PHP语言刷OJ题
平常在学校都是用C,C++,Java来刷OJ题,把AC的题用不同的语言再AC一次,基本相当于翻译而已.看到学校的OJ支持提交PHP代码,于是尝试了一下. 首先,得会使用PHP,但是你如果在看这篇博客, ...
- 8.20~8.25刷散题记录 By cellur925
记录一些散题 / 价值不大但还是想记下来的题目 / 没正八经写博客的题目 8.24 Luogu P1508 沙雕题数字三角形的二维升级版,但是注意阅读理解,李大水牛从桌子最后一行下侧开始吃,而本题是自 ...
- c语言刷 设计题合计
355. 设计推特 #define MAX_LEN 512 struct User { int userId; int followee[MAX_LEN]; // 散列表,0/1,1表示这个user被 ...
- 刷题记录:[SUCTF 2019]CheckIn
目录 刷题记录:[SUCTF 2019]CheckIn 一.涉及知识点 1.利用.user.ini上传\隐藏后门 2.绕过exif_imagetype()的奇技淫巧 二.解题方法 刷题记录:[SUCT ...
- [BUUCTF-Pwn]刷题记录1
[BUUCTF-Pwn]刷题记录1 力争从今天(2021.3.23)开始每日至少一道吧--在这里记录一些栈相关的题目. 最近更新(2021.5.8) 如果我的解题步骤中有不正确的理解或不恰当的表述,希 ...
- PE刷题记录
PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...
随机推荐
- Android开发-记账本-实现记账功能选择
制作GridView适配器,实现页面数据的变化 制作类型存储数据库,存储的主要是图片类型,类型被选中时的图片,类型未被选中时的图片. 数据库代码如下 package com.example.Utils ...
- Vue.js项目的兼容性与部署配置
一.处理兼容性问题的相关插件: 1> 解决移动端某些版本的浏览器,点击事件有3s延时触发的问题 · 安装 fastclick 依赖包:npm install fastclick --save-d ...
- 解决mybatis拦截器无法注入spring bean的问题
公司要整合rabbitmq与mybatis拦截器做一个数据同步功能. 整合过程中大部分环节都没什么问题,就是遇到了mybatis拦截器 @Intercepts(@Signature(type = Ex ...
- Android 四种方法写按钮点击事件
1.匿名内部类的方式 2. 创建一个类实现onclickListener,实现onclick方法,设置控件点击事件时传一个类的对象. 3. 让当前类实现onclickListener,设置控件点击事件 ...
- js 保存并排序输入内容
转载请注明来源:https://www.cnblogs.com/hookjc/ /* Create By:jiangcheng_15 Create Date:2012-01-32 */ functio ...
- FidBugs的使用学习
是什么? 静态代码分析器,它检查类或者JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题.Findbugs自带检测器,其中有60余种Bad practice,80余种Correctness, ...
- python基础1-类属性和实例属性
类属性就是类对象所拥有的属性,它被所有类对象的实例对象所共有,在内存中只存在一个副本,这个和C++中类的静态成员变量有点类似.对于公有的类属性,在类外可以通过类对象和实例对象访问 类属性 class ...
- LVS负载均衡群集部署——DR模式
LVS负载均衡群集部署--DR模式 1.LVS-DR概述 2.部署实验 1.LVS-DR概述: LVS-DR(Linux Virtual Server Director Server)工作模式,是生产 ...
- k8s之yaml文件详解
k8s之yaml文件详解 目录 k8s之yaml文件详解 1. k8s支持的文件格式 2. YAML语言格式 3. 查看api资源版本标签 4. 编写nginx-test.yaml资源配置清单 4.1 ...
- java常用类,包装类,String类的理解和创建对象以及StringBuilder和StringBuffer之间的区别联系
一.包装类的分类: 1.黄色部分的父类为Number 继承关系: Boolean Character 其他六个基本数据类型 2.装箱和拆箱 理解:一个例子,其他的都相同 装箱:Integer inte ...