首页 > 其他 > 详细

数据结构_反转链表_两两交换

时间:2020-02-24 23:28:53      阅读:65      评论:0      收藏:0      [点我收藏+]
class Node {
    constructor (data) {
      this.data = data
      this.next = null
    }
  }
  class LinkList {
    constructor () {
      //初始化,空链表,长度为0
      this.head = null
      this.length = 0
    }
    //追加数据
    append (data) {
      // 创建新节点
      let node = new Node(data)
      // 当前为空链表
      if (!this.head) {
        // 让head指向新节点
        this.head = node
      }else {
        // 找到尾节点
        let current = this.head
        while (current.next) {
          current = current.next
        }
        current.next = node
      }
      this.length++
    }
    // 插入数据
    insert (posi, data) {
      // 判断position是否合法
      if (posi < 0 || posi > this.length) return false
      let node = new Node(data)

      if (posi == 0) {
        node.next = this.head
        this.head = node
      }else {
        let current = this.head
        let index = 0
        while (index++ < posi - 1) {
          current = current.next
        }
        node.next = current.next
        current.next = node
      }
      this.length++
      return true
    }
    // 查找数据
    indexOf (data) {
      let current = this.head
      let index = 0
      while (current) {
        if (current.data === data) {
          return index
        }
        index++
        current = current.next
      }
      return -1
    }
    // 移除指定位置数据
    removeAt (posi) {
      // Number.isInteger()判断是否为整数
      if (posi < 0 || posi > this.length -1) return false
      if (posi == 0) {
        this.head = this.head.next
      }else {
        let current = this.head
        let index = 0
        while (index++ < posi - 1) {
          current = current.next
        }
        current.next = current.next.next
      }
      this.length--
      return true
    }
    remove (data) {
      //删除所有data的数据
      while (this.indexOf(data) != -1) {
        //删除第一个出现的data数据
        this.removeAt(this.indexOf(data))
      }
      return true
    }
    isEmpty () {
      return this.head == null
    }
    size () {
      return this.length
    }
    //反转链表
    reverseList () {
      let current = this.head
      let prev = null
      while (current) {
        let temp = current.next
        current.next = prev
        prev = current
        current = temp
      }
      this.head = prev
      return this
    }
    // 两两交换
    swap () {
      let tempNode = new Node(0)
      tempNode.next = this.head
      let prev = tempNode
      while (prev.next && prev.next.next) {
        let current = prev.next
        let buffer = prev.next.next
        prev.next = buffer
        current.next = buffer.next
        buffer.next = current
        prev = current
      }
      this.head = tempNode.next
      return this
    }
  }
  var list = new LinkList()
  list.append(1)
  list.append(2)
  list.append(3)
  list.append(4)
  list.append(5)

 

数据结构_反转链表_两两交换

原文:https://www.cnblogs.com/JunLan/p/12359305.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!