三、编码技巧

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. Jmeter 压力测试笔记(3)--脚本调试/签名/cookie/提升吞吐量/降低异常率/提升单机并发性能

    import XXXsign.Openapi2sign;---导入jar包中的签名方法 String str1 = "12121"; ---需要被签名的字段:向开发了解需要哪些哪些 ...

  2. (js描述的)数据结构[树结构1.1](11)

    1.树结构: 我们不能说树结构比其他结构都要好,因为每种数据结构都有自己特定的应用场景. 但是树确实也综合了上面的数据结构的优点(当然有点不足于盖过其他的数据结构,比如效率一般情况下没有哈希表高) 并 ...

  3. Java第七天,类的继承

    面向对象编程的三大特征: 封装.继承.多态 今天我们学习继承! 继承是多态的前提,如果没有继承就没有多态. 继承主要解决的问题就是共性抽取(将许多类共有的特性便作父类,这样可以较大程度的优化代码). ...

  4. Swing组件中URL方法获取 图标

    13.3.2 图标的使用 1.创建图标 package lll; import java.awt.*; import javax.swing.*; public class DrawIcon impl ...

  5. canvas压缩、裁切图片和格式转换的方法

    按照大小压缩图片,或者按照特定分辨率裁切图片,转为blob数据.自动处理ios中可能存在的照片偏差90°问题. 例如,获取300*300大小的头像,实现以下效果: 使用方式: <!-- 引入js ...

  6. python3(四十)datetime timestamp str

    """时间处理 """ __author__on__ = 'shaozhiqi 2019/9/25' # !/usr/bin/env pyt ...

  7. 【Tool】Windows系统安装Maven依赖管理工具

    安装Maven依赖管理工具 官网下载地址:http://maven.apache.org/download.cgi 系统环境要求: [JDK]Maven3.3版本+需要JDK1.7版本以上支持 [内存 ...

  8. Matlab学习-(4)

    1. 函数 1.1 原始方法 之前我调用函数的方法是,首先写好函数文件,然后保存,然后在主函数中调用.这种方法的不足在于会导致你的工作目录的文件太多,从而导致很乱.在网上找了一些解决方法. 1.2 本 ...

  9. 网站假死 重启NGINX无效 必须重启PHP 原因分析

    一.错误提示说明: Nginx 502 Bad Gateway:请求的PHP-CGI已经执行,但是由于某种原因(一般是读取资源的问题)没有执行完毕而导致PHP-CGI进程终止. Nginx 504 G ...

  10. Mysql列属性

    列属性又称之为字段属性在mysql中一共有6个属性:null,默认值(default),列描述(comment),主键(primary key),唯一键(unique key)和自动增长 修改数据库字 ...