第一篇终结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()

给一个单链表,删除所有节点,使headNULL

删除链表{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的更多相关文章

  1. [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 ...

  2. [LeetCode] Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  3. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  4. [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 ...

  5. [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 ...

  6. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  7. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  8. [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 ...

  9. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  10. [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 ...

随机推荐

  1. 一起了解 .Net Foundation 项目 No.23

    .Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. WorldWide Tel ...

  2. MySQL数据库二

    筛选条件 比较运算符: 等于: =  (注意!不是==)            大于等于: >=          IS NULL 不等于: !=  或  <>        小于: ...

  3. c++类模板之分文件编写问题及解决

    我们在实际项目中一般习惯头文件(.h)和源文件(.cpp)分开写,这样做的好处良多,但是如果遇到了类模板,这样可能会有一点儿问题. 我们通过一个例子来看: person.h: #pragma once ...

  4. C++语言实现顺序表

    C++语言实现顺序表 顺序表的定义及其特点 顺序表的定义是:把线性表中的所有表项按照其逻辑顺序依次存储到从计算机存储中指定存储位置开始的一块连续的存储空间中. 这样,线性表中第一个表项的存储位置就是被 ...

  5. 线程池:Execution框架

    每问题每线程:在于它没有对已创建线程的数量进行任何限制,除非对客户端能够抛出的请求速率进行限制. 下边 有些图片看不到,清看原地址:http://www.360doc.com/content/10/1 ...

  6. Kubectl patch命令使用

    kubectl patch 使用(patch)补丁修改.更新资源的字段. 支持JSON和YAML格式. 请参阅https://htmlpreview.github.io/?https://github ...

  7. 跨域cookies 共享

    这是由于,本地调试.涉及到cookies的问题 想要跨域使用的问题 vue 中的mian.js中放入下面代码 import axios from 'axios' axios.defaults.with ...

  8. Floyd-例题-实现-我的第一篇博客

    https://www.cnblogs.com/lbssxz/p/11014911.html 这是网上看到的题目,以上是原博主的解答和题目来源(没找到别的题目来源) 题目大意: 城市交通费 [问题描述 ...

  9. 用python为喜欢的人写一个程序,每天发送贴心的消息

    消息内容 包括如下: 日期(阳历+阴历): 每日壹句(内容来自爱词霸[1]): 天气预报(内容来自中国天气网[2]): 天气情况: 温度情况: 穿衣指数: 减肥指数: 空气指数: 紫外线指数: 消息效 ...

  10. MySQL不香吗,为什么还要有noSQL?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是分布式专题的第14篇文章,我们一起来看看NoSQL数据库. 其实我很早就想写写分布式数据库相关的文章,既是我现在正在学习的,也是我很感 ...