You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+15-28Lines changed: 15 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,16 +2,12 @@
2
2
# Algorithm
3
3
>Notes and codes for learning algorithm and data structures :smiley:
4
4
5
-
Some pictures and ideas are from `<<IntroductiontoAlgotithm>>
5
+
Some pictures and ideas are from `<<Introduction to Algotithm>>`
6
6
7
-
I use python 3.6+ and c++ to implements them.
8
-
Since I used f-Strings in python, you may use python 3.6+ to run the following python scripts.
9
-
10
-
>>I am still learning new things and this repo is always updating.
7
+
I use python 3.6+ and c/c++ to implement them.
11
8
12
9
# Notice
13
-
Currently, Github can't render latex math formulas.
14
-
So,if you wannt to view the notes which contain latex math formulas and are in markdown format, you can visit [my blog](https://mbinary.coding.me)
10
+
Currently, Github can't render latex math formulas.Thus,if you want to view the markodwn notes which contain latex math formulas, you can visit [my blog](https://mbinary.coding.me)
15
11
16
12
# Index
17
13
*[.](.)
@@ -21,12 +17,15 @@ So,if you wannt to view the notes which contain latex math formulas and are in m
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
+
"""
20
+
nd=self.head
21
+
foriinrange(index+1):
22
+
nd=nd.follow
23
+
ifndisNone:return-1
24
+
returnnd.val
25
+
26
+
defaddAtHead(self, val):
27
+
"""
28
+
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.
29
+
:type val: int
30
+
:rtype: void
31
+
"""
32
+
nd=node(val,self.head.follow)
33
+
self.head .follow=nd
34
+
ifself.tail.valisNone:self.tail=nd
35
+
defaddAtTail(self, val):
36
+
"""
37
+
Append a node of value val to the last element of the linked list.
38
+
:type val: int
39
+
:rtype: void
40
+
"""
41
+
self.tail.follow=node(val)
42
+
self.tail=self.tail.follow
43
+
44
+
45
+
defaddAtIndex(self, index, val):
46
+
"""
47
+
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.
48
+
:type index: int
49
+
:type val: int
50
+
:rtype: void
51
+
"""
52
+
nd=self.head
53
+
foriinrange(index):
54
+
nd=nd.follow
55
+
ifndisNone:
56
+
return
57
+
new=node(val,nd.follow)
58
+
nd.follow=new
59
+
ifself.tail==nd:
60
+
self.tail=new
61
+
62
+
63
+
defdeleteAtIndex(self, index):
64
+
"""
65
+
Delete the index-th node in the linked list, if the index is valid.
66
+
:type index: int
67
+
:rtype: void
68
+
"""
69
+
nd=self.head
70
+
foriinrange(index):
71
+
nd=nd.follow
72
+
ifndisNone:return
73
+
ifself.tail==nd.follow:self.tail=nd
74
+
ifnd.follow:nd.follow=nd.follow.follow
75
+
76
+
77
+
78
+
# Your MyLinkedList object will be instantiated and called as such:
0 commit comments