算法系列(7)leetcode 206

Reverse a singly linked list.

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    var pre = null;
   while (head) {
        var next = head.next;
        head.next = pre;
        pre = head;
        head = next;
    }
    return pre;
};


涉及了链表的知识:

链表是由一组节点组成的集合。每个节点都使用一个对象的引用指向它的后继,指向另一个节点的引用叫做链。链表一般有一个头节点。

单列表

function Node(element) {
    this.element = element;
    this.next = null;
}

function List() {       
    this.head = new Node("head");
    this.find = find;
    this.insert = insert;
    this.remove = remove;
    this.display = display;
    this.findPrevious = findPrevious;        
}

function find(item) {
    var currNode = this.head;
    while(currNode.element != item) {
        currNode = currNode.next;
    }
    return currNode;
}
function insert(newElement,item) {
    var newNode = new Node(newElement);
    var current = this.find(item);
    if(current == null) 
        return console.log("can't find the item");
    newNode.next = current.next;
    current.next = newNode;
}
function remove(item) {
    var prevNode = this.findPrevious(item);
    if(prevNode.next != null)
        prevNode.next = prevNode.next.next;

}
function findPrevious(item) {
    var currNode = this.head;
    while(currNode.next != null && currNode.next.element != item) {
        currNode = currNode.next;
    }
    return currNode;
}
function display() {
    var current = this.head;
    while(current.next != null) {
        console.log(current.next.element);
        current = current.next;
    }
}
双向列表

function LNode(element) {     //双向链表结点
    this.element = element;
    this.next = null;
    this.prev = null;
}
function LList() {           
    this.head = new LNode("head");
    this.find = find;
    this.insert = insert;
    this.remove = remove;
    this.display = display;
}
function insert(newElement.item) {
    var newNode = new Node(newElement);
    var current = this.find(item);
    newNode.next = current.next;
    current.next.prev = newNode;
    newNode.prev = current;
    current.next = newNode;
}
function remove(item) {
    var current = this.find(item);
    if(current.next != null) {
        current.prev.next = current.next;
        current.next.prev = current.prev;
        current.next = null;
        current.prev = null;
    }   
}
function findLast() {
    var currNode = this.head;
    while(current.next != null) {
        current = current.next;
    }
    return current;
}
function dispReverse() {            //反序显示双向链表的元素
    var currNode = this.findLast();
    while(currNode.prev != null) {
        print(currNode.element);
        currNode = currNode.prev;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值