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 ...
随机推荐
- Vulnhub FristiLeaks靶机渗透
VM上配置 VMware users will need to manually edit the VM's MAC address to: 08:00:27:A5:A6:76 VM上选择本靶机,编辑 ...
- .NET MVC中登录过滤器拦截的两种方法
今天给大家介绍两种ASP中过滤器拦截的两种方法. 一种是EF 的HtppModule,另一种则是灵活很多针对MVC的特性类 Attribute 具体什么是特性类可以参考着篇文章:https://www ...
- File类心得
File类心得 在程序中设置路径时会有系统依赖的问题,java.io.File类提供一个抽象的.与系统独立的路径表示.给它一个路径字符串,它会将其转换为与系统无关的抽象路径表示,这个路径可以指向一个文 ...
- excel完成数据库数据的批量插入
业务场景: 开发过程中往往会遇到这样情况就是产品直接给我们一堆数据,让咱们直接导入到后台数据库中,这时候咱们只需要在excel表中进行直接操作即可,如图所示(以河北省的编号为例): 而对应的数据表为: ...
- 安装JDK后,未设置Path,也能执行java.exe的原因
安装JDK时,自动将java.exe复制到C:\Windows\System32下
- Css3 新增的属性以及使用
Css3基础操作 . Css3? css3事css的最新版本 width. heith.background.border**都是属于css2.1CSS3会保留之前 CSS2.1的内容,只是添加了一些 ...
- 前端笔记(关于css盒模型知识整理)
我以前整理的文章可能也不是特别深入.所以现在开始尝试即使多花点时间收集整理,也不只发浅层知识,这样对技术的深入理解是很有帮助的. 废话不多说,我们现在开始. 说到css盒模型,这是大多面试基础中会经常 ...
- Hadoop权威指南(中文版-带目录索引)pdf电子书
Hadoop权威指南(中文版-带目录索引)pdf电子书下载地址:百度网盘点击下载:链接:https://pan.baidu.com/s/1E-8eLaaqTCkKESNPDqq0jw 提取码:g6 ...
- PHP修改脚本最大执行时间和最大内存限制
PHP设置脚本最大执行时间的三种方法 1.在php.ini里面设置 max_execution_time = 120; 2.通过PHP的ini_set函数设置 ini_set("max_ex ...
- 靠!安装了macOS Catalina(10.15.4)后,文件系统都乱套了
最近闲来无事,决定将我的两台apple电脑升级成最新的苹果系统(macOS Catalina),当然,由于以前升级过多次mac系统,所以毫不犹豫从app store下载了最新的macOS Cetali ...