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语言刷 链表题记录的更多相关文章

  1. C语言刷数组题记录

    讲解:https://mp.weixin.qq.com/s/weyitJcVHBgFtSc19cbPdw 二分法: 704. 二分查找 int search(int* nums, int numsSi ...

  2. c语言刷 队列题记录

    622. 设计循环队列 https://blog.csdn.net/Galaxy_n/article/details/115978544 typedef struct { int *arrs; int ...

  3. c语言刷 DFS题记录

    144. 二叉树的前序遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeN ...

  4. 用PHP语言刷OJ题

    平常在学校都是用C,C++,Java来刷OJ题,把AC的题用不同的语言再AC一次,基本相当于翻译而已.看到学校的OJ支持提交PHP代码,于是尝试了一下. 首先,得会使用PHP,但是你如果在看这篇博客, ...

  5. 8.20~8.25刷散题记录 By cellur925

    记录一些散题 / 价值不大但还是想记下来的题目 / 没正八经写博客的题目 8.24 Luogu P1508 沙雕题数字三角形的二维升级版,但是注意阅读理解,李大水牛从桌子最后一行下侧开始吃,而本题是自 ...

  6. c语言刷 设计题合计

    355. 设计推特 #define MAX_LEN 512 struct User { int userId; int followee[MAX_LEN]; // 散列表,0/1,1表示这个user被 ...

  7. 刷题记录:[SUCTF 2019]CheckIn

    目录 刷题记录:[SUCTF 2019]CheckIn 一.涉及知识点 1.利用.user.ini上传\隐藏后门 2.绕过exif_imagetype()的奇技淫巧 二.解题方法 刷题记录:[SUCT ...

  8. [BUUCTF-Pwn]刷题记录1

    [BUUCTF-Pwn]刷题记录1 力争从今天(2021.3.23)开始每日至少一道吧--在这里记录一些栈相关的题目. 最近更新(2021.5.8) 如果我的解题步骤中有不正确的理解或不恰当的表述,希 ...

  9. PE刷题记录

    PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...

随机推荐

  1. 在 Prim 算法中使用 pb_ds 堆优化

    在 Prim 算法中使用 pb_ds 堆优化 Prim 算法用于求最小生成树(Minimum Spanning Tree,简称 MST),其本质是一种贪心的加点法.对于一个各点相互连通的无向图而言,P ...

  2. 字节跳动的一道python面试题

    #!/usr/bin/python #coding=utf-8 #好好学习,天天向上 lst = ['hongkong','xiantyk','chinat','guangdong','z'] lst ...

  3. Java架构系列问题合集-目录

    接下来会做一个系列, 分类说明关于Java项目研发和架构工作需要了解的问题 Java语法 Java语法专题1: 类初始化的构造顺序 https://www.cnblogs.com/milton/p/1 ...

  4. new方法实现原理

    new方法实现原理 完整的创建一个可用的对象:Person *p=[Person new]; new方法的内部会分别调用两个方法来完成3件事情: (1)使用alloc方法来分配存储空间(返回分配的对象 ...

  5. 生成树协议(STP)的精髓知识

    STP生成树协议   1.STP介绍 2.STP生成树算法 1.STP  -   Spanning tree protocol (生成树协议)是逻辑上断开环路,防止广播风暴的产生.当线路故障,阻塞接口 ...

  6. 一文详解Kafka API

    摘要:Kafka的API有Producer API,Consumer API还有自定义Interceptor (自定义拦截器),以及处理的流使用的Streams API和构建连接器的Kafka Con ...

  7. HEAAN新版学习

    本篇文章对最新版的HEAAN库进行研究,老版的介绍见 HEAAN库学习 主要参考:slide-HEAAN.pdf HEAAN介绍 HEAAN是一个支持在加密的复数数组之间进行操作的库,方案的安全性取决 ...

  8. webfunny前端系统:如何解决警报设置触发常见问题

    大家好,经常会有小伙伴在使用webfunny监控系统中,遇到无法触发警报的问题,其实都是一些配置上的疏漏,在这里给大家总结一下: PS:只要消息中心里有警报,则说明触发机制没有问题.其他方式没有触发, ...

  9. Oracle 11G 安装详解

    oracle官网下载地址:http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html 官网下载 ...

  10. 如何对Spring MVC中的Controller进行单元测试

    对Controller进行单元测试是Spring框架原生就支持的能力,它可以模拟HTTP客户端发起对服务地址的请求,可以不用借助于诸如Postman这样的外部工具就能完成对接口的测试. 具体来讲,是由 ...