示例#1
0
#Complexity: O(n + m) where m + n are the lenghts of the 2 lists, space O(1), since no additional memory

from ListNode import ListNode

def merge_two_sorted_lists(l1: [ListNode], l2: [ListNode]) ->[ListNode]:
	# Creates a placeholder for the result
	head = ListNode()
	tail = head
	# Start building tail with the smaller value and move pointer of the list where it got used
	while l1 and l2:
		if l1.data <= l2.data:
			tail.next = l1
			l1 = l1.next
		else:
			tail.next = l2
			l2 = l2.next
		# move tail to the actual node that was smaller
		tail = tail.next
	
	# Append the remaining nodes
	tail.next = l1 or l2
	return head.next

a = ListNode(2, ListNode(5, ListNode(7)))
b = ListNode(3, ListNode(11))
a.print_list()
b.print_list()

print("Merge and Sorted:")
c = merge_two_sorted_lists(a,b)
c.print_list()
示例#2
0
# coding=utf-8
"""Delete Node in a Linked List."""

from __future__ import print_function
from ListNode import ListNode


def _solve(node):
    node.val = node.next.val
    node.next = node.next.next


if __name__ == '__main__':
    node1 = ListNode(1)
    node2 = ListNode(2)
    node3 = ListNode(3)
    node4 = ListNode(4)
    node1.next = node2
    node2.next = node3
    node3.next = node4
    _solve(node3)
    node1.print_list()