数据结构——js合并两个有序的单链表

题目:

一条链表:1 3 5 7 9 

一条链表:2 4 6 8 

最后合并为一条新链表:1 2 3 4  5 6 7 8 9

   class Lnode {
            constructor(data) {
                this.data = data;
                this.next = null;
            }
        }
        class LinkList {
            constructor() {
                this.head = null;
                this.len = 0;
            }
            // 1.append()  向链表最后添加元素
            append(ele) {
                // 创建新节点
                let newnode = new Lnode(ele);
                if (this.head == null) {
                    this.head = newnode;
                } else {
                    let current = this.head;
                    while (current.next != null) {
                        current = current.next;
                    }
                    // current表示最后一个节点
                    current.next = newnode;
                }
                this.len++;
            }
            // 2.insert(指定为位置,插入的元素)
            insert(position, ele) {
                // 位置是否合法
                if (position < 0 || position > this.len || !Number.isInteger(position)) {
                    return false
                }

                let newnode = new Lnode(ele);
                // 2.1 在头部位置插入
                if (position == 0) {
                    if (this.head == null) {
                        this.head = newnode;
                    } else {
                        newnode.next = this.head;
                        this.head = newnode;
                    }
                    this.len++;
                } else if (position == this.len) { //2.2 尾部
                    this.append(ele)
                } else { //2.3 任意位置
                    let current = this.head;
                    let index = 0;
                    // 前一个节点  position-1位置的节点
                    while (index < position - 1) {
                        current = current.next;
                        index++;
                    }
                    // current就是前一个节点
                    newnode.next = current.next;
                    current.next = newnode;
                    this.len++;
                }


            }
            // 3.removeAt(position)   移除指定位置的元素
            removeAt(position) {
                // 位置是否合法
                if (position < 0 || position > this.len - 1 || !Number.isInteger(position)) {
                    return false
                }

                if (this.head == null) { //空链表
                    return
                } else { //非空链表
                    if (position == 0) { //移除头部元素
                        this.head = this.head.next;
                    } else { //移除其他位置的元素
                        let current = this.head,
                            index = 0;
                        while (index < position - 1) {
                            current = current.next;
                            index++;
                        }

                        current.next = current.next.next;
                    }
                    this.len--;
                }

            }
            // 4.indexOf(ele)    查找指定元素的位置,存在返回index,不存在返回-1
            indexOf(ele) {
                let current = this.head,
                    index = 0;
                while (index < this.len) {
                    if (current.data == ele) {
                        return index
                    } else {
                        // 继续找
                        current = current.next;
                        index++;
                    }
                }
                return -1; // 找完了都没找到返回-1
            }

            // 5.remove(ele)  移除指定元素
            remove(ele) {
                let index = this.indexOf(ele);
                this.removeAt(index)
            }
            // 6.将链表中的数据连接为字符串
            toString() {
                let current = this.head,
                    index = 0,
                    res = "";
                while (index < this.len) {
                    res += "-" + current.data;
                    current = current.next;
                    index++;
                }
                return res.slice(1)
            }
        }
        function mergerlist(list, oterlist) {
            //1、创建新链表
            let newlist = new LinkList()
            let listHead = list.head
            let oterlistHead = oterlist.head
            //2、比较
            while (listHead && oterlistHead) {
                if (listHead.data < oterlistHead.data) {
                    newlist.append(listHead.data)
                    listHead = listHead.next
                } else {
                    newlist.append(oterlistHead.data)
                    oterlistHead = oterlistHead.next
                }
            }
            //3、list为空,另一个链表oterlist中得数据依次添加到新链表种
            while (oterlistHead) {
                newlist.append(oterlistHead.data)
                oterlistHead = oterlistHead.next
            }
            //3、otherlist为空,另一个链表otherlist中得数据依次添加到新链表中
            while (listHead) {
                newlist.append(listHead.data)
                listHead =listHead.next
            }
            console.log(newlist.toString())   //1-2-3-4-5-6-7-8-9
            return newlist
        }
        var list1 = new LinkList()
        list1.append(1)
        list1.append(3)
        list1.append(5)
        list1.append(7)
        list1.append(9)
        var list2 = new LinkList()
        list2.append(2)
        list2.append(4)
        list2.append(6)
        list2.append(8)
        // console.log(list1, list2)
        console.log( mergerlist(list1, list2))

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值