Linked List-2
三、编码技巧
1、遍历链表
先将head
指针赋值给一个局部变量current
:
//return the number of nodes in a list (while-loop version)
int Length(struct node* head)
{
int count = 0;
struct node* current = head;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
当然也可以写为:
for (current = head; current != NULL; current = current->next) {}
2、通过传递reference pointer
改变某个指针
看个例子:
//Change the passed in head pointer to be NULL
//Uses a reference pointer to access the caller's memory
void ChangeToNull(struct node** headRef) //takes a pointer to the value of interest
{
*headRef = NULL; //use * to access the value of interest
}
void ChangeCaller()
{
struct node* head1;
struct node* head2;
ChangeToNull(&head1); //use & to compute and pass a pointer to
ChangeToNull(&head2); //the value of interest
//head1 and head2 are NULL at this point
}
这块的思想是和(一)中的Push()
类似。
内存示意图:
3、通过Push()
建立链表(头插法)
这种方式的优点是速度飞快,简单易行,缺点是得到的链表是逆序的:
struct node* AddAtHead()
{
struct node* head = NULL;
for (int i = 1; i < 6; i++)
{
Push(&head, i);
}
//head == {5,4,3,2,1};
return head;
}
4、尾插法建立链表
这种方法需要找到链表最后一个节点,改变其.next
域:
- 插入或者删除节点,需要找到该节点的前一个节点的指针,改变其
.next
域; - 特例:如果涉及第一个节点的操作,那么一定要改变
head
指针。
5、特例+尾插法
如果要构建一个新的链表,那么头节点就要单独处理:
struct node* BuildWithSpecialCase()
{
struct node* head = NULL;
struct node* tail;
//deal with the head node here, and set the tail pointer
Push(&head, 1);
tail = head;
//do all the other nodes using "tail"
for (int i = 2; i < 6; i++)
{
Push(&(tail->next), i); //add node at tail->next
tail = tail->next; //advance tail to point to last node
}
return head; //head == {1,2,3,4,5}
}
6、临时节点建立
struct node* BuildWithDummyNode()
{
struct node dummy; //dummy node is temporarily the first node
struct node* tail = &dummy; //build the list on dummy.next
dummy.next = NULL;
for (int i = 1; i < 6; i++)
{
Push(&(tail->next), i);
tail = tail->next;
}
//the real result list is now in dummy.next
//dummy.next == {1,2,3,4,5}
return dummy.next;
}
7、本地指针建立
struct node* BuildWithLocalRef()
{
struct node* head = NULL;
struct node** lastPtrRef = &head; //start out pointing to the head pointer
for (int i = 1; i < 6; i++)
{
Push(lastPtrRef, i); //add node at the last pointer in the list
//advance to point to the new last pointer
lastPtrRef = &((*lastPtrRef)->next);
}
return head; //head == {1,2,3,4,5}
}
这块可能有些抽象:
1)lastPtrRef
开始指向head
指针,以后指向链表最后一个节点中的.next
域;
2)在最后加上一个节点;
3)让lastPtrRef
指针向后移动,指向最后一个节点的.next
域。 (*lastPtrRef)->next
可以理解为*lastPtrRef
指针指向的节点的next
域。
四、代码示例
1、AppendNode()
- 不使用
Push()
函数:
struct node* AppendNode(struct node** headRef, int num)
{
struct node* current = *headRef;
struct node* newNode;
newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = num;
newNode->next = NULL;
//special case for length 0
if (current == NULL)
{
*headRef = newNode;
}
else
{
//Locate the last node
while (current->next != NULL)
{
current = current->next;
}
current->next = newNode;
}
}
- 使用
Push()
函数:
struct node* AppendNode(struct node** headRef, int num)
{
struct node* current = *headRef;
//special case for length 0
if (current == NULL)
{
Push(headRef, num);
}
else
{
//Locate the last node
while (current->next != NULL)
{
current = current->next;
}
//Build the node after the last node
Push(&(current->next), num);
}
}
2、CopyList
用一个指针遍历原来的链表,两个指针跟踪新生成的链表(一个head
,一个tail
)。
- 不使用
Push()
函数:
struct node* CopyList(struct node* head)
{
struct node* current = head; //used to iterate over the original list
struct node* newList = NULL; //head of the new list
struct node* tail = NULL; //kept pointing to the last node in the new list
while (current != NULL)
{
if (newList == NULL) //special case for the first new node
{
newList = (struct node*)malloc(sizeof(struct node));
newList->data = current->data;
newList->next = NULL;
tail = newList;
}
else
{
tail->next = (struct node*)malloc(sizeof(struct node));
tail = tail->next;
tail->data = current->data;
tail->next = NULL;
}
current = current->next;
}
return newList;
}
内存示意图:
2) 使用Push()
函数:
struct node* CopyList2(struct node* head)
{
struct node* current = head; //used to iterate over the original list
struct node* newList = NULL; //head of the new list
struct node* tail = NULL; //kept pointing to the last node in the new list
while (current != NULL)
{
if (newList == NULL) //special case for the first new node
{
Push(&newList, current->data);
tail = newList;
}
else
{
Push(&(tail->next), current->data); //add each node at the tail
tail = tail->next; //advance the tail to the new last node;
}
current = current->next;
}
return newList;
}
- 使用
Dummy Node
:
struct node* CopyList3(struct node* head)
{
struct node* current = head; //used to iterate over the original list
struct node* tail = NULL; //kept pointing to the last node in the new list
struct node dummy; //build the new list off this dummy node
dummy.next = NULL;
tail = &dummy; //start the tail pointing at the dummy
while (current != NULL)
{
Push(&(tail->next), current->data); //add each node at the tail
tail = tail->next; //advance the tail to the new last node
current = current->next;
}
return dummy.next;
}
- 使用
Local References
:
struct node* CopyList4(struct node* head)
{
struct node* current = head; //used to iterate over the original list
struct node* newList = NULL; //head of the new list
struct node** lastPtr;
lastPtr = &newList; //start off pointing to the head itself
while (current != NULL)
{
Push(lastPtr, current->data); //add each node at the lastPtr
lastPtr = &((*lastPtr)->next); //advance lastPtr
current = current->next;
}
return newList;
}
核心思想是使用lastPtr
指针指向每个节点的.next
域这个指针,而不是指向节点本身。
- 使用
Recursive
:
struct node* CopyList5(struct node* head)
{
struct node* current = head;
if (head == NULL)
return NULL;
else {
struct node* newList = (struct node*)malloc(sizeof(struct node)); //make one node
newList->data = current->data;
newList->next = CopyList5(current->next); //recur for the rest
return newList;
}
}
Linked List-2的更多相关文章
- [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 ...
随机推荐
- 登录窗口java
这次代码是登录窗口的制作. 主要的方面是是包括,用户名.密码.验证码.以及输入数据所需要的文本框,对于验证码可以通过点击验证码进行修改.同时对于验证码的前景色和背景色同时都得到修改. 点击注册(这里还 ...
- Linux服务器架设篇,DHCP服务器的搭建
学习之前,我们首先来看一个案例: 假如你是一个学校的网络管理老师,需要为教室的70多台电脑配置好网络你会怎么办? 一台一台的给他们配置? 在这里我特别欣赏鸟哥的一句话--"当管理员最大的幸福 ...
- 个人项目 wc.exe
GitHub地址:https://github.com/oAiuo/wordCount 一.题目描述 Word Count1. 实现一个简单而完整的软件工具(源程序特征统计程序).2. 进行单元测试. ...
- Java成长第三集--基础重点详细说明
接上篇文章,继续阐述相关的重点基础知识,话不多说! 一.Java中equals()和“==”区别 1.对于8种基础数据类型,使用“=="比较值是否相等: 2.对于复合数据类型(类),使用eq ...
- [模板]SPFA判负环
目录 一.BFS法判负环 二.DFS法判负环 三.SPFA判正环 一.BFS法判负环 Code: #include<bits/stdc++.h> #define re register # ...
- 天天在用Redis,持久化方案你又知道哪些?
前言 文章首发于微信公众号[码猿技术专栏]:天天用Redis,持久化方案有哪些你知道吗? Redis目前已经成为主流的内存数据库了,但是大部分人仅仅是停留在会用的阶段,你真的了解Redis内部的工作原 ...
- Personal Photo Management Application
Customer Problems & Needs People may take a large number of photos and their phone don't have en ...
- adb命令查看手机应用内存使用情况
adb shell回车 一.procrank VSS >= RSS >= PSS >= USSVSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)是单个 ...
- 详解 Hashtable
至于HashTable,本人只想说,除了它们各自的特点是截然相反外,其余性质 以及 用法和HashMap的性质几乎一样, (有关Map集合的基本性质,请观看本人博文-- <详解 Map集合> ...
- Intellij IDEA 基础设置,个性化设置,好用的设置→_→
Intellij IDEA 个性化设置 Appearance & Behavior 外观和行为 Keymap 快捷键 Editor 编辑器设置 Plugins 插件 Version Contr ...