数据结构之链表🔗的设计与基本操作

152 阅读6分钟

「这是我参与2022首次更文挑战的第5天,活动详情查看:2022首次更文挑战

image.png

CURD 是日常的基本操作,链表也不外乎就是插入,删除,查找之类。

一个节点有两个属性,value 和 next,指向下一个节点的指针。

class LinkNode {
  constructor(val, next) {
    this.val = val;
    this.next = next;
  }
}

一个链表,一般有 size (大小),head (头节点),tail (尾节点)。

class MyLinkedList {
   constructor() {
      this.size = size;
      this.head = null;
      this.tail = null;
   }
}

707. 设计链表

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

  • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
  • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
  • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
  • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
class LinkNode {
  constructor(val, next) {
    this.val = val;
    this.next = next;
  }
}

var MyLinkedList = function () {
  this.size = 0;
  this.tail = null;
  this.head = null;
};

/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.getNode = function (index) {
  if (index < 0 || index >= this.size) return null;
  // 创建虚拟头节点
  let cur = new LinkNode(0, this.head);
  // 0 -> head
  while (index-- >= 0) {
    cur = cur.next;
  }
  return cur;
};

/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.get = function (index) {
  if (index < 0 || index >= this.size) return -1;
  // 获取当前节点
  return this.getNode(index).val;
};

/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function (val) {
  const node = new LinkNode(val, this.head);
  this.head = node;
  this.size++;
  if (!this.tail) {
    this.tail = node;
  }
};

/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function (val) {
  const node = new LinkNode(val, null);
  this.size++;
  if (this.tail) {
    this.tail.next = node;
    this.tail = node;
    return;
  }
  this.tail = node;
  this.head = node;
};

/**
* @param {number} index 
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function (index, val) {
  if (index > this.size) return;
  if (index <= 0) {
    this.addAtHead(val);
    return;
  }
  if (index === this.size) {
    this.addAtTail(val);
    return;
  }
  // 获取目标节点的上一个的节点
  const node = this.getNode(index - 1);
  node.next = new LinkNode(val, node.next);
  this.size++;
};

/**
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function (index) {
  if (index < 0 || index >= this.size) return;
  if (index === 0) {
    this.head = this.head.next;
    // 如果删除的这个节点同时是尾节点,要处理尾节点
    if (index === this.size - 1) {
      this.tail = this.head
    }
    this.size--;
    return;
  }
  // 获取目标节点的上一个的节点
  const node = this.getNode(index - 1);
  node.next = node.next.next;
  // 处理尾节点
  if (index === this.size - 1) {
    this.tail = node;
  }
  this.size--;
};

ok,上面都是一些基本操作。下面,让我们来提高那么一丢丢的难度。

206. 反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

 

示例 1:

输入: head = [1,2,3,4,5]
输出: [5,4,3,2,1]

示例 2:

输入: head = [1,2]
输出: [2,1]

示例 3:

输入: head = []
输出: []

image.png

image.png

image.png

var reverseList = function(head) {
  if (!head) return null
  let pre = null
  let cur = head

  while(cur !== null) {
    var temp = cur.next
    cur.next = pre
    // 改变两个指针的指向
    pre = cur
    // 走向下一个节点位置(位置信息存储在 temp 中)
    cur = temp
  }
  return pre
}

141. 环形链表

给你一个链表的头节点 head ,判断链表中是否有环。

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。

如果链表中存在环,则返回 true 。 否则,返回 false 。

示例 1:

输入: head = [3,2,0,-4], pos = 1
输出: true
解释: 链表中有一个环,其尾部连接到第二个节点。

示例 2:

输入: head = [1,2], pos = 0
输出: true
解释: 链表中有一个环,其尾部连接到第一个节点。

示例 3:

输入: head = [1], pos = -1
输出: false
解释: 链表中没有环。

设置快慢指针,如果链表有环,那么快慢指针一定会重合

var hasCycle = function (head) {
  let fast = slow = head
  while(slow && fast && fast.next) {
    slow = slow.next
    fast = fast.next.next
    if (slow === fast) {
      return true
    }
  }
  return false
}

234. 回文链表

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。

示例 1:

image.png

输入:head = [1,2,2,1]

输出:true

示例 2:

image.png

输入:head = [1,2]

输出:false

image.png

找到中心点,翻转,比较是否相同

let isPalindrome = function(head) {
  let right = reverse(findCenter(head))
  let left = head
  
  while(right !== null) {
    if (left.val !== right.val) {
      return false
    }
    left = left.next
    right = right.next
  }
  return true
}

function reverse(head) {
  if (!head) return null
  let cur = head
  let pre = null

  while(cur !== null) {
    let temp = cur.next
    cur.next = pre
    pre = cur
    cur = temp
  }

  return pre
}

function findCenter(head) {
  let faster = head, slower = head
  while(slower && faster.next && faster.next.next) {
    slower = slower.next
    faster = faster.next.next
  }
  if (faster !== null) {
    slower = slower.next
  }

  return slower
}

21. 合并两个有序链表

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

  示例 1:

输入: l1 = [1,2,4], l2 = [1,3,4]
输出: [1,1,2,3,4,4]

示例 2:

输入: l1 = [], l2 = []
输出: []

示例 3:

输入: l1 = [], l2 = [0]
输出: [0]
var mergeTwoLists = function (list1, list2) {
  let newNode = new ListNode('start');
  let temp = newNode;
  while (list1 && list2) {
    if (list1.val >= list2.val) {
      temp.next = list2
      list2 = list2.next
    } else {
      temp.next = list1;
      list1 = list1.next
    }
    temp = temp.next
  }
  temp.next = list1 === null ? list2 : list1
  return newNode.next
};

19. 删除链表的倒数第 N 个结点

难度中等1764收藏分享切换为英文接收动态反馈

给你一个链表,删除链表的倒数第 n **个结点,并且返回链表的头结点。

示例 1:

输入: head = [1,2,3,4,5], n = 2
输出: [1,2,3,5]

示例 2:

输入: head = [1], n = 1
输出: []

示例 3:

输入: head = [1,2], n = 1
输出: [1]

image.png

image.png

image.png

image.png

const removeNthFromEnd = function(head, n) {
  let pre = new ListNode(0)
  pre.next = head
  let fast = pre
  let slow = pre

  // fast 指针指向 n 的位置
  while(n--) {
    fast = fast.next
  }

  // 等 fast 走到最后一个位置的时候,slow 就在删除节点的前一个节点
  while(fast && fast.next) {
    fast = fast.next
    slow = slow.next
  }
  // 删除节点
  slow.next = slow.next.next

  return pre.next
}

160. 相交链表

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。

图示两个链表在节点 c1 开始相交

 

示例 1:

输入: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
输出: Intersected at '8'
解释: 相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A[4,1,8,4,5],链表 B[5,6,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

示例 2:

输入: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出: Intersected at '2'
解释: 相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A[1,9,1,2,4],链表 B[3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

示例 3:

输入: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出: null
解释: 从各自的表头开始算起,链表 A[2,6,4],链表 B[1,5]。
由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
这两个链表不相交,因此返回 null 。

image.png

image.png

image.png

image.png

function getIntersectionNode(headA, headB) {
  let pointA = headA;
  let pointB = headB;

  while (pointA || pointB) {
    if (pointA === pointB) {
      return pointA;
    }

    pointA = pointA === null ? headA : pointA.next;
    pointB = pointB === null ? headB : pointB.next;
  }

  return null;
}

链表的题目还有很多很多,这些都是基本的练习。大家学习的时候,可以多多画图。

参考文章:

代码随想录