• API
• FAQ
• Tools
• Archive
daily pastebin goal
45%
SHARE
TWEET

# Untitled

a guest Jul 11th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class Node:
2.     def __init__(self, val):
3.         self.val = val
4.         self.next = None
5.
6.
8.     def __init__(self):
9.         """
10.         Initialize your data structure here.
11.         """
13.
14.     def get(self, index):
15.         """
16.         Get the value of the index-th node in the linked list. If the index is invalid, return -1.
17.         :type index: int
18.         :rtype: int
19.         """
21.
22.         while index:
23.             if index == 0 and cur:
24.                 return cur.val
25.             index -= 1
26.
27.             if cur:  # if cur then go cur.next
28.                 cur = cur.next
29.             else:  # cur is None then the index is invalid
30.                 return -1
31.
33.         """
34.         Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
35.         :type val: int
36.         :rtype: void
37.         """
41.
43.         """
44.         Append a node of value val to the last element of the linked list.
45.         :type val: int
46.         :rtype: void
47.         """
49.         if cur:  # non-empty linked list
50.             while cur.next:
51.                 cur = cur.next
52.             cur.next = Node(val)
53.         else:  # empty linked list
55.
57.         """
58.         Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
59.         :type index: int
60.         :type val: int
61.         :rtype: void
62.         """
63.
64.         if index == 0:
66.             return
67.
69.         prev = None
70.         while index and cur:
71.             prev = cur
72.             cur = cur.next
73.             index -= 1
74.         if prev:
75.             if cur:
76.                 new = Node(val)
77.                 new.next = cur
78.                 prev.next = new
79.             else:
80.                 prev.next = Node(val)
81.
82.     def deleteAtIndex(self, index):
83.         """
84.         Delete the index-th node in the linked list, if the index is valid.
85.         :type index: int
86.         :rtype: void
87.         """
88.
90.         prev = None
91.         while index and cur:
92.             prev = cur
93.             cur = cur.next
94.             index -= 1
95.         if prev and cur:
96.             prev.next = cur.next
97.
98.     def __repr__(self):
99.         s = ''
101.         while cur:
102.             s += str(cur.val) + ' -> '
103.             cur = cur.next
105.
106.
107. import time
108.
110. # params = [[24],[1],[79],[10],[51],[3,37],[92],[53],[6],[5],[6],[33],[47],[7,42],[6,40],[82],[9],[37],[6,84],[2,50],[21],[22],[6],[51],[7],[10],[2],[3,5],[8],[15],[6],[89],[32],[99],[65],[51],[9],[15,62],[20],[83],[10],[3],[10],[3],[21],[22],[22],[7],[87],[73],[85],[19,17],[29],[2],[23],[88],[21],[6],[20,46],[4],[38],[59],[36],[36],[32,99],[23,79],[84],[82],[89],[98],[48],[75],[55],[4,24],[90],[8],[24],[7],[17],[37],[10],[32],[15],[9],[18],[19],[53,96],[31],[11],[96],[4,44],[69],[65],[33,22],[38,55],[31,31],[12],[0],[47],[65],[54,37]]
111.