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 ...
随机推荐
- Thinkphp6源码分析之解析,Thinkphp6路由,Thinkphp6路由源码解析,Thinkphp6请求流程解析,Thinkphp6源码
Thinkphp6源码解析之分析 路由篇-请求流程 0x00 前言: 第一次写这么长的博客,所以可能排版啊,分析啊,什么的可能会比较乱.但是我大致的流程已经觉得是说的够清楚了.几乎是每行源码上都有注释 ...
- Java第一天,带你走进编程的世界,我的第一个程序
要想彻底了解Java是什么,我就得首先了解编程语言的发展史.编程语言最初的形势是"0101......"数据编程,也就是机器语言.机器语言可以说是一种几乎没有人能够看懂的编程语言, ...
- C语言 文件操作(三)
1.fputs() int fputs(const char *s, FILE *stream); s 代表要输出的字符串的首地址,可以是字符数组名或字符指针变量名. stream 表示向何种流中输出 ...
- 《闲扯Redis四》List数据类型底层编码转换
一.前言 Redis 提供了5种数据类型:String(字符串).Hash(哈希).List(列表).Set(集合).Zset(有序集合),理解每种数据类型的特点对于redis的开发和运维非常重要. ...
- template_showpost
使用<a href='...'>name<\a>实现点击"name"与转向'...'网址的超链接操作 from django.shortcut import ...
- 2020年iOS进阶面试题总结(一)
准备找工作的你,可以看看,复习复习!! 1.说一下OC的反射机制 在动态运行下我们可以构建任何一个类,然后我们通过这个类知道这个类的所有的属性和方法,并且如果我们创建一个对象,我们也可以通过对象找到这 ...
- AJ学IOS 之CoreLocation指南针小应用
AJ分享,必须精品 一:效果图示 简单的用到CoreLocation获取方位做的指南针小应用 二:制作思路 具体用到了CoreLocation相关的知识,请看上一篇博客有写 然后获取方向不需要进行授权 ...
- 第一章:shell脚本初入门
1.shell脚本中的source或者.空格再加上文件,表示加载文件中的命令及语句(困惑多时终于解开^-^) 2.脚本开头书写好作者版本等信息,方便维护:流程语句提前把格式写好,防止遗漏 3.定义字符 ...
- 控件:DataGridView列类型
DataGridView的列的类型提供有多种,包括有: (1)DataGridViewTextBoxColumn(文本列,默认的情况下就是这种) (2)DataGridViewComboBoxColu ...
- PAS
一.概念 二.安装 打开Delphi,在主菜单上选择Component,单击Install Component,出现图所示的对话框.有两个选择,装到已经存在的包里面和装到新的包里面.我们选择后者,单击 ...