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个素数,处理出这些素 ...
随机推荐
- ORACLE数据库误操作DELETE并且提交数据库之后如何恢复被删除的数据
一:根据时间来恢复: 1.查询数据库当前时间() select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; 2.查询删除数据时间点之前的数据 ...
- Redis 源码简洁剖析 03 - Dict Hash 基础
Redis Hash 源码 Redis Hash 数据结构 Redis rehash 原理 为什么要 rehash? Redis dict 数据结构 Redis rehash 过程 什么时候触发 re ...
- 常见线程池 newFixedThreadPool 的简单使用
package com.aaa.threaddemo; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurr ...
- oracle 相关查询和非相关查询,oracle 去除重复数据,以及oracle的分页查询!
一.oracle中的相关查询?和非相关查询? 二.oracle去除重复数据 1. 2. 3.oracle 实现分页? 利用rownum的唯一性,和子查询,将rownum从伪列变成实际列!
- 阿里四面:你知道Spring AOP创建Proxy的过程吗?
Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...
- SpringBoot 自定义配置
有时候需要自己定义一些配置,比如SpringBoot没有提供Druid连接池的配置,需要我们自己写配置. 以在springboot中使用Druid为例. 依赖 <dependency> & ...
- java基础之抽象类的介绍
抽象类的特点: 1.当方法只有声明没有具体实现的时候,需要用abstract修饰符修饰.抽象方法必须定义在抽象类当中,所以抽象类也需要用abstract修饰 2.抽象类不可以被实例化,为什么呢? ...
- Posix 信号
转载请注明来源:https://www.cnblogs.com/hookjc/ 函数sem_open创建一个新的有名信号灯或打开一个已存在的有名信号灯.有名信号灯总是既可用于线程间的同步,又可以用于进 ...
- js判断当前浏览设备
前端开发经常遇到需要判断用户的浏览设备,是pc端还是移动端,移动端使用的是什么手机系统?android.ios.ipad.windows phone等等,有时候还需要知道用户浏览页面是在微信中打开还是 ...
- 【转】Nginx服务并发过10万的Linux内核优化配置
http://www.linuxidc.com/Linux/2012-11/75151.htm以下Linux 系统内核优化配置均经在线业务系统测试,服务器运行状态良好,用了一些时间整理,现和大家分享一 ...