Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
linked_lists_2
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
class Node(object): def __init__(self, data=None): self.data = data self.next = None class Context(object): def __init__(self, first, second): self.first = first self.second = second def alternating_split(head): #if type(head) is Node: if head == None or len(head) == 1: raise Exception first = [v for i, v in enumerate(head) if i%2 == 0] second = [v for i, v in enumerate(head) if i%2 != 0] return Context(first, second) def push(head, data): newNode = Node(data) newNode.next = head return newNode def build_one_two_three(): head = None head = push(head, 3) head = push(head, 2) head = push(head, 1) return head def build_one_two(): head = None head = push(head, 2) head = push(head, 1) return head def build_one_two_three_four_five_six(): head = None head = push(head, 6) head = push(head, 5) head = push(head, 4) head = push(head, 3) head = push(head, 2) head = push(head, 1) return head def build_list(data): data.reverse() head = None for num in data: head = push(head, num) return head def assert_linked_list_equals(listA, listB, message): while listA is not None and listB is not None: test.assert_equals(listA.data, listB.data, message) listA = listA.next listB = listB.next def length(node): len = 0 if node != None: current = node while current.next != None: current = current.next len += 1 return len + 1 return len def count(node, data): count = 0 if node != None: current = node if node.data == data: count +=1 while current.next != None: current = current.next if current.data == data: count +=1 return count def get_nth(node, index): if node != None: ind = 0 current = node if index == 0: return current.data while current.next != None: current = current.next ind += 1 if ind == index: return current.data return None #подсмотрено def get_nth1(head, index): if not head: raise ValueError('Head node cannot be None') if index < 0: raise ValueError('Index cannot be negative') if index > length(head) - 1: raise ValueError('Index cannot exceed number of nodes in list') counter = 0 current_node = head while counter != index: current_node = current_node.next counter += 1 return current_node print('______Insert Nth Node_____') def insert_nth(head, index, data): if index > length(head)-1: raise Exception('Index') if index == 0: newNode = Node(data) newNode.next = head return newNode count = 0 curr = head while curr != None: count += 1 if count == index: newNode = Node(data) if curr.next.next != None: newNode.next = curr.next.next curr.next = newNode break curr = curr.next return curr def iterNode(head): curr = head arr = [] #for i in range(length(head)): while curr: arr.append(curr.data) curr = curr.next return arr nn = build_one_two_three_four_five_six() nn = push(nn, 23) nn = insert_nth(nn, 0, 7) nn = insert_nth(nn, 1, 8) print(iterNode(nn)) nn = insert_nth(nn, 3, 42) print(iterNode(nn))
[
+
]
Show input
Absolute running time: 0.7 sec, cpu time: 0.61 sec, memory peak: 6 Mb, absolute service time: 0,7 sec
edit mode
|
history
|
discussion
______Insert Nth Node_____ [7, 8, 1, 2, 3, 4, 5, 6] [1, 42, 3, 4, 5, 6]