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. WebSphere--WAS概念和原理解析

    WebSphere--WAS概念和原理解析--tigergao收录于2021/04/25

  2. 手把手教你用Strace诊断问题

    手把手教你用Strace诊断问题 发表于2015-10-16 早些年,如果你知道有个 strace 命令,就很牛了,而现在大家基本都知道 strace 了,如果你遇到性能问题求助别人,十有八九会建议你 ...

  3. 学习Java第13天

    今天选择数据库选了半天,Oracle,MySQL,SQL sever太难了,安装了又被图形界面,Linux虚拟机所困扰 明天尽量完成数据库安装 只能说是找视频资料和安装教程了.

  4. react组件中的类调用construcor、super方法你知道多少?

    constructor:在类中作为一个钩子函数,有constructor钩子函数的时候,可以定义state,如果用户不定义state的话,有无constructor钩子函数没啥区别: super:

  5. 「SHOI2006」有色图

    首先发现这题虽然是边的置换,但是是由点的置换所造成的,并且发现点置换对应的所有边置换和置换操作构成置换群. 由于颜色可以全选,那么根据 Polya 定理,答案为: \[|X / G| = \frac{ ...

  6. rpm与yum安装及管理程序

    安装及管理程序 1.Linux应用程序基础 2.RPM软件包管理工具 3.yum源仓库创建 1.应用程序与系统命令的关系如图:  典型应用程序的目录结构如图: 常见的软件包封装类型如图: 2.RPM包 ...

  7. 微信小程序开发常用功能

    获取用户信息 调用 wx.getUserProfile 方法获取用户基本信息.页面产生点击事件(例如 button 上 bindtap 的回调中)后才可调用,每次请求都会弹出授权窗口,用户同意后返回 ...

  8. C++实现对Json数据的友好处理

    背景 C/C++客户端需要接收和发送JSON格式的数据到后端以实现通讯和数据交互.C++没有现成的处理JSON格式数据的接口,直接引用第三方库还是避免不了拆解拼接.考虑到此项目将会有大量JSON数据需 ...

  9. 5个不常提及的HTML技巧

    2021年你需要知道的HTML标签和属性 Web开发人员都在广泛的使用HTML.无论你使用什么框架或者选择哪个后端语言,框架在变,但是HTML始终如一.尽管被广泛使用,但还是有一些标签或者属性是大部分 ...

  10. ybt的坑

    emmmm ybt 字符串处理 例2题解错了 AC自动机板子错了(据说) 另外字符串处理的题解写的我一脸懵逼 网站上eeeee 点击查看E. 1.排队接水 #include <iostream& ...