-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16a5f68
commit c79c9c3
Showing
7 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import random | ||
import numpy as np | ||
|
||
|
||
def selection_sort(sequence): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 题目描述 | ||
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。 | ||
|
||
|
||
# 解题思路 | ||
|
||
1. listNode 是链表,只能从头遍历到尾,但是输出却要求从尾到头,这是典型的"先进后出",我们可以想到栈! | ||
2. Python中有个方法是 insert(index,value),可以指定 index 位置插入 value 值。 | ||
3. 在遍历 ListNode 的同时将每个遇到的值插入到新list中的 0 位置(头位置),最后输出 新的list 即可得到逆序链表 | ||
|
||
![](从尾到头打印链表.jpg) | ||
# 总结 | ||
时间复杂度 O(N),空间复杂度 O(N)。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class ListNode: | ||
def __init__(self, x): | ||
self.val = x | ||
self.next = None | ||
|
||
|
||
def printListFromTailToHead(listNode): | ||
""" | ||
返回从尾部到头部的列表值序列,例如[1,2,3] | ||
:param listNode: | ||
:return: | ||
""" | ||
pTmp = listNode | ||
ret_array = [] | ||
while pTmp: | ||
ret_array.insert(0, pTmp.val) # 每次都往临时数组的头部添加 | ||
pTmp = pTmp.next | ||
print(ret_array) | ||
return ret_array | ||
|
||
|
||
if __name__ == '__main__': | ||
l1 = ListNode(1) | ||
l2 = ListNode(2) | ||
l3 = ListNode(3) | ||
l4 = ListNode(4) | ||
l5 = ListNode(5) | ||
l1.next = l2 | ||
l2.next = l3 | ||
l3.next = l4 | ||
l4.next = l5 | ||
printListFromTailToHead(l1) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 题目描述 | ||
输入一个链表,输出该链表中倒数第k个结点。 | ||
|
||
# 解题思路 | ||
1. 定义两个指针,第一个指针从链表的头部开始遍历向前走k-1步, 第二个指针暂时保持不动 | ||
2. 当第一个指针走到K步时, 第二个指针开始从链表的头部开始遍历 | ||
3. 当第一个(走在前面的)指针到达链表的尾结点时,第二个指针(走在后面)正好是倒数第K个结点 | ||
|
||
![](链表中倒数的第k个结点.png) | ||
|
||
# 总结 | ||
倒数第k个,就是正向的n(链表的长度) - k |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class ListNode: | ||
def __init__(self, x): | ||
self.val = x | ||
self.next = None | ||
|
||
|
||
def FindKthToTail(head, k): | ||
"""输入一个链表,输出该链表中倒数第k个结点。 | ||
:param head: 头结点 | ||
:param k: 倒数第k个结点 | ||
:return: | ||
""" | ||
# 边界条件: k 如果比链表长度大 ,直接返回None | ||
# K 如果小于链表长度,定义两个变量, 这两个变量中间间隔k | ||
firstedPoint = head | ||
secondPoint = head | ||
|
||
for i in range(k): | ||
if firstedPoint == None: | ||
return None | ||
firstedPoint = firstedPoint.next | ||
|
||
while firstedPoint != None: | ||
firstedPoint = firstedPoint.next | ||
secondPoint = secondPoint.next | ||
|
||
print(secondPoint.val) | ||
return secondPoint | ||
|
||
|
||
if __name__ == '__main__': | ||
l1 = ListNode(1) | ||
l2 = ListNode(2) | ||
l3 = ListNode(3) | ||
l4 = ListNode(4) | ||
l5 = ListNode(5) | ||
l1.next = l2 | ||
l2.next = l3 | ||
l3.next = l4 | ||
l4.next = l5 | ||
FindKthToTail(l1, 3) | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.