链表
链表中的节点在内存中不是连续分布的 ,而是散乱分布在内存中的某地址上,分配机制取决于操作系统的内存管理。
链表分为单链表和双链表:
- 单链表:每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针)。单链表中的指针域只能指向节点的下一个节点。
- 双链表:每一个节点有两个指针域,一个指向下一个节点,一个指向上一个节点。
- 循环链表:顾名思义,就是链表首尾相连。
链表与数组的比较:
- 定义数组时长度是固定的,链表的长度不固定。
- 数组适合频繁的查询,链表适合频繁增删。
//链表的定义
private class ListNode {
// 结点的值
int val;
// 下一个结点
ListNode next;
// 节点的构造函数(无参)
public ListNode() {
}
// 节点的构造函数(有一个参数)
public ListNode(int val) {
this.val = val;
}
// 节点的构造函数(有两个参数)
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
第一题:203. 移除链表元素 - 力扣(LeetCode)
//设置虚拟头节点
public ListNode removeElements(ListNode head, int val) {
//一个空链表不需要任何操作
if (head == null) {
return head;
}
//因为删除可能涉及头节点,所以设置dummy节点,统一操作
ListNode dummy = new ListNode(-1, head); //是处理可能需要删除头节点的情况
ListNode pre = dummy; //记录当前节点的前一个节点
ListNode cur = head; //遍历链表
while (cur != null) { //空链表不需要任何操作
if (cur.val == val) {
pre.next = cur.next; //pre的下一个节点指向cur的下一个节点
} else {
pre = cur; //将pre更新为cur
}
cur = cur.next; //将cur更新为下一个节点
}
return dummy.next; //返回dummy的下一个节点,这才是实际的头节点
}
class MyLinkedList {
//手写一个单链表
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
}
//size存储链表元素的个数
int size;
//虚拟头结点
ListNode head;
//初始化链表
public MyLinkedList() {
size = 0;
head = new ListNode(0);
}
//获取第index个节点的数值,注意index是从0开始的,第0个节点就是头结点
public int get(int index) {
//如果index非法,直接返回-1
if (index < 0 || index > size - 1) {
return -1;
}
ListNode currentNode = head;
//包含一个虚拟节点,所以查找第index + 1 个节点
for (int i = 0; i <= index; i++) {
currentNode = currentNode.next;
}
return currentNode.val;
}
//在链表最前面插入一个节点,等价于在第0个元素前添加
public void addAtHead(int val) {
addAtIndex(0, val);
}
//在链表的最后插入一个节点,等价于在(末尾+1)个元素前添加
public void addAtTail(int val) {
addAtIndex(size, val);
}
// 在第 index 个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。
// 如果 index 等于链表的长度,则说明是新插入的节点为链表的尾结点
// 如果 index 大于链表的长度,则返回空
public void addAtIndex(int index, int val) {
if (index > size) {
return;
}
if (index < 0) {
index = 0;
}
size++;
//找到要插入节点的前驱
ListNode pred = head;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
ListNode toAdd = new ListNode(val);
toAdd.next = pred.next;
pred.next = toAdd;
}
//删除第index个节点
public void deleteAtIndex(int index) {
if (index < 0 || index >= size) {
return;
}
size--;
if (index == 0) {
head = head.next;
return;
}
ListNode pred = head;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
pred.next = pred.next.next;
}
}
public ListNode reverseList(ListNode head) {
ListNode prev = null; //记录当前节点的前一个节点
ListNode cur = head; //遍历链表
ListNode temp = null; //用于交换链表
while (cur != null) {
temp = cur.next;// 保存下一个节点
cur.next = prev;
prev = cur;
cur = temp;
}
return prev;
}
第四题:24. 两两交换链表中的节点 - 力扣(LeetCode)
public ListNode swapPairs(ListNode head) {
ListNode dumyhead = new ListNode(-1); // 设置一个虚拟头结点
dumyhead.next = head; // 将虚拟头结点指向head,这样方便后面做删除操作
ListNode cur = dumyhead;
ListNode temp; // 临时节点,保存两个节点后面的节点
ListNode firstnode; // 临时节点,保存两个节点之中的第一个节点
ListNode secondnode; // 临时节点,保存两个节点之中的第二个节点
while (cur.next != null && cur.next.next != null) {
temp = cur.next.next.next;
firstnode = cur.next;
secondnode = cur.next.next;
cur.next = secondnode; // 步骤一
secondnode.next = firstnode; // 步骤二
firstnode.next = temp; // 步骤三
cur = firstnode; // cur移动,准备下一轮交换
}
return dumyhead.next;
}
第五题:19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
ListNode fastIndex = dummyNode;
ListNode slowIndex = dummyNode;
// 只要快慢指针相差 n 个结点即可
for (int i = 0; i < n + 1; i++) {
fastIndex = fastIndex.next;
}
//同时移动,直到fast指向末尾
while (fastIndex != null) {
fastIndex = fastIndex.next;
slowIndex = slowIndex.next;
}
//此时 slowIndex 的位置就是待删除元素的前一个位置。
slowIndex.next = slowIndex.next.next;
return dummyNode.next;
}
第六题:面试题 02.07. 链表相交 - 力扣(LeetCode)(困难)
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;
}
第七题:142. 环形链表 II - 力扣(LeetCode)(困难)
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; //慢指针:一次走一个节点
fast = fast.next.next; //快指针:一次走两个节点
if (slow == fast) {// 有环
ListNode index1 = fast; //快慢指针相遇的点
ListNode index2 = head; //头节点
// 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
while (index1 != index2) {
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}