Skip to content

pydsa.linked

build_linked_list

build_linked_list(vals)
Source code in pydsa/linked.mu
3
4
5
6
7
8
def build_linked_list(vals)
  dummy = it = ListNode()
  for v in vals
    it.next = ListNode(v)
    it = it.next
  dummy.next
 6
 7
 8
 9
10
11
def build_linked_list(vals):
    dummy = it = ListNode()
    for v in vals:
        it.next = ListNode(v)
        it = it.next
    return dummy.next

print_linked_list

print_linked_list(head) -> None
Source code in pydsa/linked.mu
10
11
12
13
14
15
16
17
18
def print_linked_list(head) -> none
  seen = set()
  while head and id(head) not in seen
    seen.add(id(head))
    print(head.val, end=head.next ? " -> " : "")
    head = head.next
  if head
    print(f" -> [{head.val}] (cycle)")
  print ""
14
15
16
17
18
19
20
21
22
def print_linked_list(head) -> None:
    seen = set()
    while head and id(head) not in seen:
        seen.add(id(head))
        print(head.val, end=" -> " if head.next else "")
        head = head.next
    if head:
        print(f" -> [{head.val}] (cycle)")
    print("")

get_list_values

get_list_values(head)
Source code in pydsa/linked.mu
20
21
22
23
24
25
def get_list_values(head)
  ret vals, seen = [], set()
  while head and id(head) not in seen
    seen.add(id(head))
    vals <- head.val
    head = head.next
25
26
27
28
29
30
31
def get_list_values(head):
    vals, seen = [], set()
    while head and id(head) not in seen:
        seen.add(id(head))
        vals.append(head.val)
        head = head.next
    return vals

draw_linked_list

draw_linked_list(head) -> None
Source code in pydsa/linked.mu
27
28
29
30
31
32
33
34
def draw_linked_list(head) -> none
  if not head
    print "Empty linked list"
    return
  from PrettyPrint import PrettyPrintLinkedList
  pt = PrettyPrintLinkedList(x -> x.val, x -> x.next, x -> none,
    orientation=PrettyPrintLinkedList.Horizontal, color="\x1b[44m")
  pt(head)
34
35
36
37
38
39
40
def draw_linked_list(head) -> None:
    if not head:
        print("Empty linked list")
        return
    from PrettyPrint import PrettyPrintLinkedList
    pt = PrettyPrintLinkedList(lambda x: x.val, lambda x: x.next, lambda x: None, orientation=PrettyPrintLinkedList.Horizontal, color="\x1b[44m")
    pt(head)

linked_list_reverse_k

linked_list_reverse_k(
    h: Optional[ListNode], k: float = inf
) -> tuple[Optional[ListNode], Optional[ListNode]]

Reverse the first k nodes of the list at h, in place.

Returns the new head and the new tail. The tail is the node that was h, and its next is the first node that was not reversed. With k left out, or a list shorter than k, the whole list is reversed and the tail's next is None.

head, tail = linked_list_reverse_k(build_linked_list([1, 2, 3, 4, 5]), 3) get_list_values(head), tail.val ([3, 2, 1, 4, 5], 1)

Source code in pydsa/linked.mu
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def linked_list_reverse_k(h: ListNode?, k: float=inf) -> (ListNode?, ListNode?)
  """Reverse the first k nodes of the list at h, in place.

  Returns the new head and the new tail. The tail is the node that was h,
  and its next is the first node that was not reversed. With k left out,
  or a list shorter than k, the whole list is reversed and the tail's next
  is None.

  >>> head, tail = linked_list_reverse_k(build_linked_list([1, 2, 3, 4, 5]), 3)
  >>> get_list_values(head), tail.val
  ([3, 2, 1, 4, 5], 1)
  """
  d, t = ListNode(), h
  while h and k > 0
    d.next, h.next, h = h, d.next, h.next
    k -= 1
  if t
    t.next = h
  d.next, t
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def linked_list_reverse_k(h: Optional[ListNode], k: float = inf) -> tuple[Optional[ListNode], Optional[ListNode]]:
    """Reverse the first k nodes of the list at h, in place.

    Returns the new head and the new tail. The tail is the node that was h,
    and its next is the first node that was not reversed. With k left out,
    or a list shorter than k, the whole list is reversed and the tail's next
    is None.

    >>> head, tail = linked_list_reverse_k(build_linked_list([1, 2, 3, 4, 5]), 3)
    >>> get_list_values(head), tail.val
    ([3, 2, 1, 4, 5], 1)
    """
    d, t = ListNode(), h
    while h and k > 0:
        d.next, h.next, h = h, d.next, h.next
        k -= 1
    if t:
        t.next = h
    return d.next, t