在Python中合并两个链接列表

时间:2018-09-28 15:02:40

标签: python python-3.x

合并两个排序的链表,并将其作为新列表返回。应该通过将前两个列表的节点拼接在一起来创建新列表。

我几乎完成了。我无法正常工作。任何使它起作用的提示将不胜感激。

class node():
    def __init__(self, data, next_node):
        self.data = data
        self.next_node = None

    def get_data(self):
        return self.data

    def get_next(self):
        return self.next_node

    def set_next(self, new_node):
        self.next_node = new_node

class LinkList(object):

    def __init__(self, head = None):
        self.head = head

    def add(self, data):
        new_node = node(data)
        new_node.set_next(self.head)
        self.head = new_node

    def printlist(self):
        current_node = self.head
        while (current_node != None):
            print(current_node.get_data(), end="->")
            current_node = current_node.get_next()


def merge(L1, L2):

    temp = None

    if L1 is None:
        return L2

    if L2 is None:
        return L1

    if L1.data <= L2.data:
        temp = L1
        temp.next_node = merge(L1.next_node, L2)

    else:
        temp = L2
        temp.next_node = merge(L1,L2.next_node)

        return temp


def main():

    list1 = LinkList()
    list1.add(10)
    list1.add(20)
    list1.add(30)

    list2 = LinkList()
    list2.add(10)
    list2.add(30)
    list2.add(50)

    list3 = LinkList()

    list3.head = merge(list1.head, list2.head)
    print("merge link list: ", end=" " )
    list3.printlist()
if __name__ == "__main__":
    main()

2 个答案:

答案 0 :(得分:0)

您仅传递数据参数,因此将next_node的默认值设置为无

class node:
    def __init__(self, data, next_node=None):
        self.data = data
        self.next_node = None

下面是升序排序的解决方案

class Node(object):
    def __init__(self, data):
        self.data = data
        self.next = None

    def __str__(self):
        return "<Node>{}</Node> {}".format(self.data, self.next)


class LinkList(object):
    def __init__(self):
        self.current = self.head = None

    def add(self, node):
        if not self.head:
            self.head = self.current = node
        else:
            self.current.next = node
            self.current = node

    def __str__(self):
        return "<head>{}</head> {}".format(self.head.data, self.head.next)


def merge(lst1, lst2):
    rtn = LinkList()
    while lst1 and lst2:
        if lst1.data < lst2.data:
            rtn.add(Node(lst1.data))
            lst1 = lst1.next
        else:
            rtn.add(Node(lst2.data))
            lst2 = lst2.next
    if lst1:
        rtn.add(lst1)
    if lst2:
        rtn.add(lst2)
    return rtn


def main():
    lst = LinkList()
    lst.add(Node(1))
    lst.add(Node(2))
    lst.add(Node(3))
    lst2 = LinkList()
    lst2.add(Node(1.1))
    lst2.add(Node(2.1))
    lst2.add(Node(3.1))
    rtn = merge(lst.head, lst2.head)
    print(rtn)


if __name__ == '__main__':
    main()

答案 1 :(得分:0)

由于您要添加到列表的开头,所以:

var dataArray = [23, 13];
var names = [ "Category1", "Category2" ];
var widths = [ "50", "700" ];

var svg = d3.select("svg.d3svg")
  .attr("height", "20%")
  .attr("width", "100%")

var bar = svg.selectAll("g")
  .data(dataArray)
  .enter().append("g")

var gradient = svg
    .append("linearGradient")
    .attr("y1", "0%")
    .attr("y2", "100%")
    .attr("x1", "0%")
    .attr("x2", "100%")
    .attr("id", "gradient")
    .attr("gradientUnits", "userSpaceOnUse")

gradient
    .append("stop")
    .attr('class', 'start')
    .attr("offset", "0%")
    .attr("stop-color", "red")
    .attr("stop-opacity", 1);

gradient
    .append("stop")
    .attr('class', 'end')
    .attr("offset", "100%")
    .attr("stop-color", "green")
    .attr("stop-opacity", 1);

var rect = bar.append('rect')
  .attr("height", "7")
  .attr("width", function(d, i) { return widths[i] })
  .attr("y", function(d, i) { return (i * 40) + 30 })
  .attr("x", "0")
  .attr("stroke", "url(#gradient)")

var text = bar.append('text')
  .attr("class", "text-svg")
  .text (function(d, i) { return names[i] })
  .attr("x", "0")
  .attr("y", function(d, i) { return (i * 40) + 50 });

var text = bar.append('text')
  .attr("class", "text-svg-caption")
  .text ("Text for this goes here")
  .attr("x", "135")
  .attr("y", "120");

这意味着您的列表被颠倒了。因此,您必须在合并过程中进行反向检查:

def add(self, data):
    new_node = node(data)
    new_node.set_next(self.head)
    self.head = new_node

全文:

if L1.data >= L2.data: