三、编码技巧

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()

  1. 不使用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;
}
}
  1. 使用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)。

  1. 不使用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;
}
  1. 使用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;
}
  1. 使用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域这个指针,而不是指向节点本身。

  1. 使用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的更多相关文章

  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. Redis对象——有序集合(ZSet)

    有序集合类型 (Sorted Set或ZSet) 相比于集合类型多了一个排序属性 score(分值),对于有序集合 ZSet 来说,每个存储元素相当于有两个值组成的,一个是有序结合的元素值,一个是排序 ...

  2. sublime text3添加并修改编译系统

    版权声明:本文为CSDN博主「肥宅_Sean」的原创文章,遵循 CC 4.0 BY-SA 版权协议,原文链接 方法工具 -> 编译系统 -> 新建编译系统 按ctrl+s保存.(注意,这里 ...

  3. Springboot系列(七) 集成接口文档swagger,使用,测试

    Springboot 配置接口文档swagger 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配 ...

  4. 全网最全C#实习面试题目

    整个内容是我在春招面试时候整理的一些题目,里面涵盖有网上搬运的(由于当时没有记录来源,如果有转载没标注来源,请与我联系),还有我面试到的.整个排版很乱,后期我会一步一步整理.整个内容大概快有两万字.整 ...

  5. Eclipse版本控制

    各版本的区别: 1.Eclipse IDE for Java Developers 是Eclipse的platform加上JDT插件,用来java开发的 2.Eclipse IDE for Java  ...

  6. 移动硬盘临时文件太多怎么办,python黑科技帮你解决

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 星安果 PS:如果想了解更多关于python的应用,可以私信我,或者 ...

  7. L19深度学习中的优化问题和凸性介绍

    优化与深度学习 优化与估计 尽管优化方法可以最小化深度学习中的损失函数值,但本质上优化方法达到的目标与深度学习的目标并不相同. 优化方法目标:训练集损失函数值 深度学习目标:测试集损失函数值(泛化性) ...

  8. 数据结构之循环队列Demo

    循环队列 比较简单,循环队列主要是判断队满.队空.有效元素个数 画图说明: 假设:队的长度为5(0-4) 但是实际maxsize为6,需要一个预留空间(不存储元素)做计算 继续添加3个元素后: 出队一 ...

  9. 基于Python的Webservice开发(四)-泛微OA的SOAP接口

    一.功能需求 泛微e-cology可以在流程中调用Webservice接口实现与其他系统的联动等复杂功能.但是目前泛微文档中仅提供了调用的方法,但是没有关于接口的相关开发信息. 本次案例是用Pytho ...

  10. Mysql数据导入导出功能(设置及使用)

    使用Mysql自带的outfile语法,将查询结果导成excel格式. 1.OUTFILE介绍及常见问题解决: )查询数据导出成csv 直接使用mysql导出csv方法 我们可以使用 into out ...