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. 使用 Kubeadm+Containerd 部署一个 Kubernetes 集群

    本文独立博客阅读地址:https://ryan4yin.space/posts/kubernetes-deployemnt-using-kubeadm/ 本文由个人笔记 ryan4yin/knowle ...

  2. 学习JAVAWEB第十四天

    ## JSP:入门学习 1. 概念: * Java Server Pages: java服务器端页面 * 可以理解为:一个特殊的页面,其中既可以指定定义html标签,又可以定义java代码 * 用于简 ...

  3. JavaScript之ES6常用新特性

    参考:https://www.jianshu.com/p/ac1787f6c50f 变量声明:const 与 let const:常量,必须初始化值   let:变量 格式:const 变量A = & ...

  4. oracle 快速创建用户

    create user  testdb identified by 123456; grant  dba to testdb;

  5. Caffeine缓存 最快缓存 内存缓存

    一.序言 Caffeine是一个进程内部缓存框架. 对比Guava Cache Caffeine是在Guava Cache的基础上做一层封装,性能有明显提高,二者同属于内存级本地缓存.使用Caffei ...

  6. JavaScript闭包的那些事

    JavaScript闭包 1.函数在JavaScript中的地位 在介绍闭包之前,可以先聊聊函数在JavaScript中的地位,因为闭包的存在是与函数息息相关的. JavaScript之所以可以称之为 ...

  7. ajax、axios、fetch区别及优缺点

    将jQuery的ajax.axios和fetch做个简单的比较,所谓仁者见仁智者见智,最终使用哪个还是自行斟酌 1.jQuery ajax $.ajax({ type: 'POST', url: ur ...

  8. 使用python实现冒泡排序和快速排序

    1 def bubble(arr): 2 """冒泡排序""" 3 loop = len(arr) - 1 4 if loop > 0 ...

  9. Ansible 自动化运维管理工具

    Ansible 自动化运维管理工具 1.Ansible概述 2.Ansible部署 3.Ansible模块 1.Ansible概述: Ansible是一个基于Python开发的配置管理和应用部署工具, ...

  10. Solution -「AGC 003D」「AT 2004」Anticube

    \(\mathcal{Description}\)   Link.   给定 \(n\) 个数 \(a_i\),要求从中选出最多的数,满足任意两个数之积都不是完全立方数.   \(n\le10^5\) ...