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

相關文章
相關標籤/搜索