一、理论基础
(1)链表的类型:单向链表、双向链表、循环链表
(2)链表的存储方式:链表中的节点在内存中不是连续分布的 ,而是散乱分布在内存中的某地址上,分配机制取决于操作系统的内存管理。
(3)节点的定义:
struct ListNode{
int val;
ListNode *next;
ListNode(int x): val(x),next(null) {}
};
(4)链表的操作
删除:
插入:
数组长度一经定义,就是固定的,支持随机访问。数组适用于频繁查询,增删少的场景;
链表的长度可以不固定,支持动态增删,适用于增删频繁,查询少的场景。
二、链表问题常见操作及思考方向
1.移除链表元素
链表的一大问题就是操作当前节点必须要找前一个节点才能操作。这就造成了,头结点的尴尬,因为头结点没有前一个节点了。
每次对应头结点的情况都要单独处理,所以使用虚拟头结点的技巧,就可以解决这个问题。
即,在链表前设置一个dummyNode,往往能够使操作一致,避免了各种操作对头结点的单独讨论:
ListNode dummyNode = new ListNode();
dummyNode.next = head;
public ListNode removeElements(ListNode head, int val) {
// 设置一个虚拟的头结点
ListNode dummy = new ListNode();
dummy.next = head;
ListNode cur = dummy;
while (cur.next != null) {
if (cur.next.val == val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return dummy.next;
}
2.设计链表,这道题覆盖了链表的常见操作
class ListNode{
int val;
ListNode next;
ListNode(int val){
this.val = val;
}
}
class MyLinkedList {
int size;
ListNode head;
public MyLinkedList() {
size = 0;
head = new ListNode(0);
}
public int get(int index) {
if(index < 0 || index >= size){
return -1;
}
ListNode cur=head;
for(int i = 0; i <= index ; i++){
cur = cur.next;
}
return cur.val;
}
public void addAtHead(int val) {
ListNode node = new ListNode(val);
node.next = head.next;
head.next = node;
size++;
}
public void addAtTail(int val) {
ListNode cur = head;
while(cur.next != null){
cur = cur.next;
}
ListNode node = new ListNode(val);
cur.next=node;
size++;
}
public void addAtIndex(int index, int val) {
if(index == size ){
addAtTail(val);
return;
}
if(index > size) return ;
ListNode cur = head;
for(int i = 0; i < index ; i++){
cur = cur.next;
}
ListNode node = new ListNode(val);
node.next = cur.next;
cur.next = node;
size++;
return ;
}
public void deleteAtIndex(int index) {
if(index <0 || index >= size){
return;
}
ListNode cur = head;
for(int i = 0; i < index ; i++){
cur = cur.next;
}
if (cur.next != null) {
cur.next = cur.next.next;
}
size--;
return ;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/
3.反转链表
从这道题分析这类链表题的一般考虑方向:
(1)在纸上画出链表具体情况,根据题意确定操作顺序,从而确定一次迭代的操作中的顺序,以及需要的tmp结点
(2)考虑dummyNode是否能够减少需要考虑的边界情况
(3)考虑具体的判断条件、边界情况,考虑返回值
题目:两两交换链表中的节点
这道题体现了上面说的考虑过程,用上面说的过程进行模拟。
三、链表问题技巧
1.快慢指针(也属于刷题记录1中介绍到的双指针技巧)
如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyNode = new ListNode(-1);
dummyNode.next=head;
ListNode fast=dummyNode , slow=dummyNode;
for(int i = 0; i< n; i++){
fast=fast.next;
}
while(fast.next != null){
fast=fast.next;
slow=slow.next;
}
slow.next=slow.next.next;
return dummyNode.next;
}
}
题目:链表相交
这道题更像一道思维题
(版本一)先行移动长链表实现同步移动
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0, lenB = 0;
while (curA != null) { // 求链表A的长度
lenA++;
curA = curA.next;
}
while (curB != null) { // 求链表B的长度
lenB++;
curB = curB.next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
//1. swap (lenA, lenB);
int tmpLen = lenA;
lenA = lenB;
lenB = tmpLen;
//2. swap (curA, curB);
ListNode tmpNode = curA;
curA = curB;
curB = tmpNode;
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
while (gap-- > 0) {
curA = curA.next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != null) {
if (curA == curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
}
(版本二) 合并链表实现同步移动
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// p1 指向 A 链表头结点,p2 指向 B 链表头结点
ListNode p1 = headA, p2 = headB;
while (p1 != p2) {
// p1 走一步,如果走到 A 链表末尾,转到 B 链表
if (p1 == null) p1 = headB;
else p1 = p1.next;
// p2 走一步,如果走到 B 链表末尾,转到 A 链表
if (p2 == null) p2 = headA;
else p2 = p2.next;
}
return p1;
}
}
2.环形链表
在环形链表中往往经常使用到快慢指针。
本题需要推导一个公式,根据快慢指针来做:
那么相遇时: slow指针走过的节点数为: x + y
, fast指针走过的节点数:x + y + n (y + z)
,n为fast指针在环内走了n圈才遇到slow指针, (y+z)为 一圈内节点的个数A。
因为fast指针是一步走两个节点,slow指针一步走一个节点, 所以 fast指针走过的节点数 = slow指针走过的节点数 * 2:
(x + y) * 2 = x + y + n (y + z)
两边消掉一个(x+y): x + y = n (y + z)
因为要找环形的入口,那么要求的是x,因为x表示 头结点到 环形入口节点的的距离。
所以要求x ,将x单独放在左面:x = n (y + z) - y
,
再从n(y+z)中提出一个 (y+z)来,整理公式之后为如下公式:x = (n - 1) (y + z) + z
注意这里n一定是大于等于1的,因为 fast指针至少要多走一圈才能相遇slow指针。