第一篇终结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. gdb调试工具常用命令 && kdb

    编译程序时需要加上-g,之后才能用gdb进行调试:gcc -g main.c -o main gdb中命令: 回车键:重复上一命令 (gdb)help:查看命令帮助,具体命令查询在gdb中输入help ...

  2. VUE_shop(第十天)项目的打包优化

    项目的打包优化 1.添加页面的加载效果 1.首先安装运行依赖nprocess,在main.js文件中的axios拦截器拦截请求的时候调用Npeocees.start. 在拦截响应的时候调用nproce ...

  3. Java第十七天,Set接口

    Set接口 1.特点 (1)不包含重复元素. (2)没有索引. (3)继承自Collection接口,所以Collection接口中的所有方法都适用于Set接口. 2.解析 (1)为什么不能包含重复元 ...

  4. bfs和dfs辨析—基础复习(从stack和queue的角度来理解区别,加深理解,不再模糊)

    参考: https://www.cnblogs.com/Tovi/articles/6194815.html https://blog.csdn.net/dangzhangjing97/article ...

  5. Python—一个简单搜索引擎索引库

    因为课业要求,搭建一个简单的搜索引擎,找了一些相关资料并进行了部分优化(坑有点多) 一.数据 数据是网络上爬取的旅游相关的攻略页面 这个是travels表,在索引中主要用到id和url两个字段. 页面 ...

  6. 惊呆了,Servlet Filter和Spring MVC Interceptor的实现居然这么简单

    前言 创建型:单例模式,工厂模式,建造者模式,原型模式 结构型:桥接模式,代理模式,装饰器模式,适配器模式,门面模式,组合模式,享元模式 行为型:观察者模式,模板模式,策略模式,责任链模式,状态模式, ...

  7. Problem L. World Cup

    题目大意:有A,B,C,D四个队伍,两两之间有一个比赛,假如A和B比赛,如果平局,各加一分,如果说A胜,给A加3分,不给B加分,B胜同理 给出A,B,C,D,的得分,判断形成这种局面有多少种方式. 思 ...

  8. linux常用命令--文件搜索

    find / -name file1 从 '/' 开始进入根文件系统搜索文件和目录 find / -user user1 搜索属于用户 'user1' 的文件和目录 find /home/user1 ...

  9. vue-element-admin执行npm install 报错

    如果你出现这类报错: 那么恭喜你,因为这个问题很好解决. ----------------------- 解决方法: git config --global url."https://&qu ...

  10. java 一维数组的总结笔记

    数组 1. 一位数组的声明方式 type[] array Name 或 type arrayName[];(推荐使用第二种) 错误的声明方式 //int[5] intErrorArray;错误的 // ...