合并两条有序链表

封装LinkedList方法

class Node {
    constructor(data) {
        this.data = data
        this.next = null
    }
}

class LinkedList {
    constructor() {
        this.length = 0
        this.head = null
    }
    append(ele) {
        let newnode = new Node(ele)
        if (this.head == null) {
            this.head = newnode;
            this.length++
        } else {
            let current = this.head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newnode;
            this.length++
        }
    }
    insert(position, ele) {
        if (position < 0 || position > this.length || !Number.isInteger(position)) {
            return false
        }
        var newnode = new Node(ele)
        if (position == 0) {
            if (this.head == null) {
                this.head = newnode;
            } else {
                newnode.next = this.head
                this.head = newnode
            }
            this.length++;
        } else if (position == this.length) {
            this.append(ele)
        } else {
            let current = this.head;
            let index = 0;
            while (index < position - 1) {
                current = current.next;
                index++;
            }
            newnode.next = current.next;
            current.next = newnode;
            this.length++
        }
    }
    removeAt(position) {
        if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
            return false
        }
        if (this.head == null) {
            return false
        } else {
            if (position == 0) {
                this.head = this.head.next
            } else {
                let current = this.head;
                let index = 0
                while (index < position - 1) {
                    current = current.next
                    index++
                }
                current.next = current.next.next
            }
            this.length--
        }
    }
    indexof(ele) {
        let current = this.head;
        let index = 0
        while (index < this.length) {
            if (current.data == ele) {
                return index
            } else {
                current = current.next
                index++
            }

        }
        return -1
    }
    remove(ele) {
        let index = this.indexof(ele)
        this.removeAt(index)
    }
    toString() {
        let current = this.head,
            index = 0,
            res = "";
        while (index < this.length) {
            res += "-" + current.data;
            current = current.next;
            index++;
        }
        return res.slice(1)
    }
    isEmpty() {
        if (this.length > 0) {
            return false
        } else {
            return true
        }
    }
    size() {
        return this.length
    }
}

 

 

 

用JavaScript实现合并两条有序链表:

 const link1 = new LinkedList()
        link1.append(1)
        link1.append(3)
        link1.append(4)
        link1.append(6)
        link1.append(7)


        const link2 = new LinkedList()
        link2.append(2)
        link2.append(5)
        link2.append(8)
        link2.append(9)

        function merginlist(list, otherlist) {
            //1、创建新链表
            let newlist = new LinkedList();

            let listHead = list.head
            let otherlistHead = otherlist.head;

            //2、比较
            while (listHead && otherlistHead) {
                if (listHead.data < otherlistHead.data) {
                    newlist.append(listHead.data)
                    listHead = listHead.next
                } else {
                    newlist.append(otherlistHead.data)
                    otherlistHead = otherlistHead.next
                }

            }
            //3.list为空,另一个链表otherlist中的数据依次添加到新链表中
            while (otherlistHead) {
                newlist.append(otherlistHead.data)
                otherlistHead = otherlistHead.next
            }

            // 3.otherlist为空,另一个链表list中的数据依次添加到新链表中
            while (listHead) {
                newlist.append(listHead.data)
                listHead = listHead.next
            }

            return newlist.toString()
        }
                
        console.log(merginlist(link1, link2));

 

posted on 2022-08-24 23:21  香香鲲  阅读(13)  评论(0编辑  收藏  举报