单项链表的定义
- 单向链表单链表是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始.
- 特点: 链表是由结点构成,head指针指向第一个成为表头结点,而终止于最后一个指向NULL的指针。
常用功能代码实现
function SingleLink(){
this.head = null
this.length = 0
//用于生成链表结构中的一个元素类
function ItemLink(data){
this.data = data
this.next = null
}
// 1.向链表后端追加一个元素
SingleLink.prototype.append = function(data){
let newItem = new ItemLink(data)
if(this.length === 0){
this.head = newItem
}else{
let current = this.head
while(current.next){
current = current.next
}
currnet.next = newItem
}
this.length += 1
}
// 2.插入指定位置
SingleLink.prototype.insert = function(position, data){
// 边界判断
if (position > this.length || position < 0) return false
let newItem = new ItemLink(data)
// 插入开头位置
if(this.length === 0){
this.head = newItem
}else{
if(position === 0){
newItem.next = this.head
this.head = newItem
}else{
let index = 0
let previous = null
let current = this.head
while(index++ < positon){
previous = current
current = current.next
}
newItem.next = current
previous.next = newItem
}
}
this.length += 1
}
// 3.获取对应位置的元素
SingleLink.prototype.get = function(position){
// 边界判断
if (position > this.length || position < 0) return null
let index = 0
let current = this.head
while(index++ < position){
current = currnet.next
}
return current.data
}
// 4.返回元素在链表中的位置
SingleLink.prototype.indexOf = function(data){
let index = 0
let current = this.head
while(current){
if(current.data == data){
return index
}
current = current.next
index += 1
}
reurn -1
}
// 5.修改某一位置的元素
SingleLink.prototype.update = function(position, data){
// 判断越界
if (position < 0 || position > this.length - 1) return null
let index = 0
let current = this.head
while (index++ < position) {
previous = current
current = current.next
}
current.data = data
}
// 6.根据位置信息移除某一元素
SingleLink.prototype.removeAt = function(position){
// 判断越界
if (position < 0 || position > this.length - 1) return null
let current = this.head
if(position === 0){
current = this.head.next
this.head = current
}else{
let index = 0
let previous = null
while(index++ < position){
previous = current
current = current.next
}
if(position === this.length - 1){
previous.next = null
}else{
previous.next = current.next
}
}
this.length -= 1
return current.data
}
// 7.根据data从链表中移除某一项
SingleLink.prototype.remove = function(data){
let currentIndex = this.indexOf(data)
return this.removeAt(currentIndex)
}
// 8. 链表是否为空
SingleLink.prototype.isEmpty = function(){
return this.length === 0
}
// 9.链表的长度
SingleLink.prototype.size = function(){
return this.length
}
// 10. toSring
SingleLink.prototype.toString = function(){
let current = this.head
let str = ''
while(current){
str += current.data + ''
current = current.next
}
return str
}
// 11.反转链表
SingleLink.prototype.reverLink = function(){
// this.head = this.reverLinkNode(this.head)
this.head = this.IterativeNode(this.head)
}
// 递归调用实现反转链表
SingleLink.prototype.reverLinkNode = function(node){
if(node == null || node.next == null) {
return node
}
let newNode = this.reverLinkNode(node.next)
node.next.next = node
node.next = null
return newNode
}
// Iterative 方式实现反转列表(也就是按顺序从头到尾遍历一遍)
SingleLink.prototype.IterativeNode = function(node){
let pre = null
let cur = node
while(cur != null){
let nxt = cur.next
// 三步走
cur.next = pre
pre = cur
cur = nxt
}
return pre
}
}
实例应用
- 给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
- 请你将两个数相加,并以相同形式返回一个表示和的链表。
- 你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
var addTwoNumbers = function(l1, l2) {
if(l1 == null){
return l2
}
if(l2 == null){
return l1
}
l2.val = l1.val + l2.val
if(l2.val >= 10){
l2.val = l2.val % 10
if(l2.next != null){
l2.next.val = l2.next.val + 1
if(l2.next.val === 10){
l2.next.val = addTwoNumbers(new ListNode(0, null), l2.next).val
}
}else{
l2.next = new ListNode(1, null)
}
}
l2.next = addTwoNumbers(l1.next, l2.next)
return l2
}