js 算法4

又来一个算法
题目:去除链表中重复的元素,并返回修改后的链表。node

  • 1-1-3-3-4-5 这种普通递归的解决 去重1跟3
function Node(value) {
    this.value = value
    this.next = null
}
Node.prototype.setNext = function(node) {
    this.next = node
    return node
}
var head = new Node(1)
head.setNext(new Node(1)).setNext(new Node(3)).setNext(new Node(3)).setNext(new Node(4)).setNext(new Node(5))
//这里建立了一个链表   113345
console.log("返回", removeNode(head))   //1345  
function removeNode(head) {
    if(!head) {
        return null
    }
    var nextnode = head.next //搞一个下一个的指针
    if(head.value === nextnode.value) { //看下value是否相等
        head.next = nextnode.next    //将head下一个指向到nextnode的下一个
        removeNode(head)  //递归head  这时候head一遍找到去掉第一个重复的数据
    } else {
        //若是不相等了
        removNode(head.next)  //这里不相等说明前面的去掉了重复  后面用head.next 来接着递归搞
    }
    return head 
}复制代码
  • 尾递归 至于啥叫尾递归 下面单独写一遍 这里就简单说程序最后一步调用自身
function Node(value) {
    this.value = value
    this.next = null
}
Node.prototype.setNext = function(node) {
    this.next = node
    return node
}
var head = new Node(1)
head.setNext(new Node(1)).setNext(new Node(3)).setNext(new Node(3)).setNext(new Node(4)).setNext(new Node(5))

function removeNode(head, pre = null, res = null) {  //这里添加两个变量  pre是前一个节点  res 是最后要返回的链表头 因此加这个参数 
    if(!head) {    
        return res
    }
    res = res || head
    if(pre.value === head.value) {
        pre.next = head.next
    } else {
        pre = head
    }
    reurn removeNode(head.next,pre, res)
}复制代码
  • 循环的解法
function Node(value) {
    this.value = value
    this.next = null
}
Node.prototype.setNext = function(node) {
    this.next = node
    return node
}
var head = new Node(1)
head.setNext(new Node(1)).setNext(new Node(3)).setNext(new Node(3)).setNext(new Node(4)).setNext(new Node(5))

console.log(removeNode(head))
function removeNode(head) {
    if(!head) {
        return null
    }
    var pre = head  //前一个节点是头结点
    var cur = head.next //当前节点是头结点下一个
    var next = null
    while (cur) {  //开始循环当前节点
      next = cur.next
      if(pre.value === cur.value) {  //比较是否相等 
          pre.next = next   //将pre  cur  next  连接起来
          cur = next
      } else {
          pre = cur
          cur = next  
      }
      return head
    }
}复制代码

上面都是有序 若是没有顺序那 好比 131345 来看下解法算法

  • 无序链表 解法是拿第一个跟后面比较重复就删 而后拿第二个跟后面比较 这次类推
    function Node(value) {
      this.value = value
      this.next = null
    }
    Node.prototype.setNext = function(node) {
      this.next = node
      return node
    }
    var head = new Node(1)
    head.setNext(new Node(1)).setNext(new Node(3)).setNext(new Node(3)).setNext(new Node(4)).setNext(new Node(5))
    console.log(removeNode(head))
    function removeNode(head) {
      if(!head) {
          return null
      }
      var pre = null
      var cur = head
      var next = null  //前一个节点  当前节点  下一个节点
      while (cur!=null) {     //拿第一个节点
          pre = cur
          next = cur.next   //下一个节点 cur.next
          while (next!=null) {    //里面嵌套一个循环来搞从cur.next开始跟pre(pre = cur)比较 
              if(pre.value === next.value) {
                  pre.next = next.next
              } else {
                  pre = next
              }
              next = next.next
          }
          cur = cur.next
      }
      return head
    }复制代码

以上就是 有序 无序的解法 有好的解法能够补充bash

相关文章
相关标签/搜索