Linked List-3
第一篇终结Linked List(一)、终结Linked List(二)主要讲了单链表的基础知识,接下来的第二篇主要讲一些比较经典的问题。
一、Count()
给一个单链表和一个整数,返回这个整数在链表中出现了多少次。
/*
Given a list and an int, return the number of times
that int ocucurs in the list.
*/
int Count(struct node* head,int searchFor)
{
int cnt = 0;
struct node* cur = head;
while (cur != NULL)
{
if (cur->data == searchFor)
cnt++;
cur = cur->next;
}
return cnt;
}
也可以用for循环实现。
二、GetNth()
给一个单链表和一个index,返回index位置上的数值,类似array[index]操作。
/*
Given a list and an index, return the data in the nth
node of the list. The nodes are numbered from 0.
Assert fails if the index is invalid (outside 0..length - 1).
*/
int GetNth(struct node* head,int index)
{
int len = 0;
struct node* cur = head;
while (cur)
{
if (len == index)
{
return cur->data;
}
cur = cur->next;
len++;
}
assert(0); //如果走到这一行,表达式的值为假,断言失败
}
三、DeleteList()
给一个单链表,删除所有节点,使head为NULL。
删除链表{1,2,3}的示意图:

void DeleteList(struct node** headRef)
{
struct node* cur = *headRef; //deref headRef to get the real head
while (*headRef)
{
cur = *headRef;
*headRef = cur->next;
free(cur);
}
}
四、Pop()
给一个链表,删掉头节点,返回头节点的数据。
内存示意图:

/*
The opposite of Push().Takes a non-empty list and
remove the front node, and returns the data which was in that node.
*/
int pop(struct node** headRef)
{
assert(*headRef != NULL);
int ans = (*headRef)->data; //pull out the data before the node is deleted
struct node* cur = *headRef;
*headRef = (*headRef)->next; //unlink the head node for the caller
free(cur); //free the head node
return ans;
}
五、InsertNth()
可以在[0,length]的任意位置插入指定元素。
/*
A more general version of Push().
Given a list, an index 'n' in the range 0..length,
and a data element, add a new node to the list so that
it has the given index.
*/
void InsertNth(struct node** headRef,int index,int data)
{
//position 0 is a special case
if (index == 0)
{
Push(headRef, data);
}
else
{
int cnt = 0;
struct node* cur = *headRef;
while (cnt < index - 1)
{
assert(cur != NULL); //if this fails, the index was too big
cur = cur->next;
cnt++;
}
assert(cur != NULL); //tricky:you have to check one last time
Push(&(cur->next), data);
}
}
这段代码坑有点多,可以通过画图或者单步跟踪的方法调试。
InsertNthTest()可以用来测试:
void InsertNthTest()
{
struct node* head = NULL; //start with the empty list
InsertNth(&head, 0, 13); //{13}
InsertNth(&head, 1, 42); //{13,42}
InsertNth(&head, 1, 5); //{13,5,42}
}
六、SortedInsert()
给定一个有序链表和一个节点,将该节点插入到合适的位置。
共有三种方法:
1、Uses special case code for the head end
void SortedInsert(struct node** headRef,struct node* newNode)
{
//Special case for the head end
if (newNode->data <= (*headRef)->data || *headRef == NULL)
{
newNode->next = *headRef;
*headRef = newNode;
}
else
{
//Locate the node before the point of insertion
struct node* cur = *headRef;
while (cur->next && cur->next->data < newNode->data)
{
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
}
}
2、Dummy node strategy for the head end
用dummy node这种方法一般不需要处理特殊情况。
void SortedInsert2(struct node** headRef,struct node* newNode)
{
struct node dummy;
struct node* cur = &dummy;
dummy.next = *headRef;
while (cur->next && newNode->data >= cur->next->data)
{
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
*headRef = dummy.next; //头指针永远指向dummy.next
}
3、Local references strategy for the head end
void SortedInsert3(struct node** headRef,struct node* newNode)
{
struct node** curRef = headRef;
while (*curRef && (*curRef)->data <= newNode->data)
{
curRef = &((*curRef)->next);
}
newNode->next = *curRef; //Bug:(*curRef)->next is incorrect
*curRef = newNode;
}
Linked List-3的更多相关文章
- [LeetCode] Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [LeetCode] Plus One Linked List 链表加一运算
Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...
- [LeetCode] Odd Even Linked List 奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- [LeetCode] Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
随机推荐
- android的volley学习
更简单的一种方式是在build.gradle中引入依赖[推荐这种方式] compile 'com.android.volley:volley:1.1.1' StringRequest的用法接下来我们看 ...
- python selenium使用
安装selenium #Python pip install selenium #Anaconda3 conda install selenium 下载浏览器版本对应的驱动文件 chrome chro ...
- matplotlib BlendedGenericTransform(混合变换)和CompositeGenericTransform(复合变换)
2020-04-10 23:31:13 -- Edit by yangrayBlendedGenericTransform是Transform的子类,支持在x / y方向上使用不同的变换.(博主翻译为 ...
- hadoop(十一)HDFS简介和常用命令介绍
HDFS背景 随着数据量的增大,在一个操作系统中内存不了了,就需要分配到操作系统的的管理磁盘中,但是不方便管理者维护,迫切需要一种系统来管理多态机器上的文件,这就是分布式文件管理系统. HDFS的概念 ...
- 挑战全网最幽默的Vuex系列教程:第一讲 Vuex到底是什么鬼
先说两句 官方已经有教程了,为什么还要写这个教程呢?说实话,还真不是我闲着蛋疼,官方的教程真的是太官方了,对于刚入门 Vuex 的童鞋来说,想必看官方的教程,很多地方就如同看圣经一样,比如「欧玛尼玛尼 ...
- 基于linux或windows平台上的c/s简单通信
linux: tcpclient.cpp #include<iostream> #include<unistd.h> #include<sys/types.h> # ...
- Python中有许多HTTP客户端,但使用最广泛且最容易的是requests
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:北京尚脑软件测试 PS:如有需要Python学习资料的小伙伴可以加点击 ...
- H - Hamiltonian Hypercube Gym - 101170H
规律题 首先我们要知道他的顺序是怎么来的,首先当n等于1时,是0,1 当n=2时,先按照与按顺序在他们前面分别加0,即00,01,在逆序加1,即11,10 构成的顺序为00,01,11,10:往后同理 ...
- element-ui修改自定义主题
官方文档:https://element.eleme.cn/#/zh-CN/component/custom-theme 简单更换主题色 打开:在线主题编辑器,仅修改主题色,点击右上角[切换主题色], ...
- JavaScript中一种全新的数据类型-symbol
连续连载了几篇<ES6对xxx的扩展>,本节咱们换换口味,介绍一种全新的数据类型:Symbol,中文意思为:标志,记号.音标:[ˈsɪmbəl]. 数据类型 在介绍Symbol之前,我们简 ...