File tree 7 files changed +167
-0
lines changed
7 files changed +167
-0
lines changed Original file line number Diff line number Diff line change @@ -23,4 +23,21 @@ class Solution:
23
23
slow = slow.next
24
24
fast = fast.next.next
25
25
return True
26
+ ```
27
+
28
+ ```
29
+ class Solution(object):
30
+ def hasCycle(self, head):
31
+ """
32
+ :type head: ListNode
33
+ :rtype: bool
34
+ """
35
+
36
+ slow = fast = head
37
+ while fast and fast.next:
38
+ slow = slow.next
39
+ fast = fast.next.next
40
+ if slow == fast:
41
+ return True
42
+ return False
26
43
```
Original file line number Diff line number Diff line change
1
+ *** 给定一个链表,返回链表开始入环的第一个节点。如果链表无环,则返回 null。***
2
+
3
+ ```
4
+ # Definition for singly-linked list.
5
+ # class ListNode:
6
+ # def __init__(self, x):
7
+ # self.val = x
8
+ # self.next = None
9
+
10
+ class Solution:
11
+ def detectCycle(self, head: ListNode) -> ListNode:
12
+ slow = fast = head
13
+ flag = False
14
+ while fast and fast.next:
15
+ fast = fast.next.next
16
+ slow = slow.next
17
+ if fast == slow:
18
+ flag = True
19
+ break
20
+ if not flag:
21
+ return None
22
+
23
+ fast = head
24
+ while fast != slow:
25
+ fast = fast.next
26
+ slow = slow.next
27
+
28
+ return fast
29
+ ```
Original file line number Diff line number Diff line change
1
+ *** 给定一个头结点为 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。***
2
+
3
+ ```
4
+ # Definition for singly-linked list.
5
+ # class ListNode:
6
+ # def __init__(self, val=0, next=None):
7
+ # self.val = val
8
+ # self.next = next
9
+ class Solution:
10
+ def middleNode(self, head: ListNode) -> ListNode:
11
+ slow = fast = head
12
+ while fast and fast.next:
13
+ fast = fast.next.next
14
+ slow = slow.next
15
+ return slow
16
+ ```
Original file line number Diff line number Diff line change
1
+ *** 给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。***
2
+
3
+ ```
4
+ # Definition for singly-linked list.
5
+ # class ListNode(object):
6
+ # def __init__(self, val=0, next=None):
7
+ # self.val = val
8
+ # self.next = next
9
+ class Solution(object):
10
+ def isPalindrome(self, head):
11
+ """
12
+ :type head: ListNode
13
+ :rtype: bool
14
+ """
15
+
16
+ if not head:
17
+ return False
18
+ if not head.next:
19
+ return True
20
+
21
+ # 用于找到中间点的指针
22
+ slow = head
23
+ fast = head
24
+ # 用于反转的指针
25
+ pre = None
26
+ cur = head
27
+
28
+ while fast and fast.next:
29
+ slow = slow.next
30
+ fast = fast.next.next
31
+ cur.next = pre
32
+ pre = cur
33
+ cur = slow
34
+
35
+ #奇数情况
36
+ if fast:
37
+ slow = slow.next
38
+
39
+ while pre and slow:
40
+ if pre.val != slow.val:
41
+ return False
42
+ pre = pre.next
43
+ slow = slow.next
44
+ return True
45
+ ```
Original file line number Diff line number Diff line change
1
+ *** 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。***
2
+
3
+ ```
4
+ # Definition for singly-linked list.
5
+ # class ListNode(object):
6
+ # def __init__(self, x):
7
+ # self.val = x
8
+ # self.next = None
9
+
10
+ class Solution(object):
11
+ def reversePrint(self, head):
12
+ """
13
+ :type head: ListNode
14
+ :rtype: List[int]
15
+ """
16
+ #方法一:递归
17
+ if not head:
18
+ return []
19
+ rev = self.reversePrint(head.next)
20
+ rev.append(head.val)
21
+ return rev
22
+ #方法二
23
+ #res = []
24
+ #while head:
25
+ # res.insert(0, head.val)
26
+ # head = head.next
27
+ #return res
28
+ ```
Original file line number Diff line number Diff line change
1
+ *** 请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。***
2
+
3
+ ![ algo35] ( ./images/algo35.jpg )
4
+
5
+ ```
6
+ class Solution:
7
+ def copyRandomList(self, head: 'Node') -> 'Node':
8
+ if not head: return
9
+ cur = head
10
+ # 1. 复制各节点,并构建拼接链表
11
+ while cur:
12
+ tmp = Node(cur.val)
13
+ tmp.next = cur.next
14
+ cur.next = tmp
15
+ cur = tmp.next
16
+ # 2. 构建各新节点的 random 指向
17
+ cur = head
18
+ while cur:
19
+ if cur.random:
20
+ cur.next.random = cur.random.next
21
+ cur = cur.next.next
22
+ # 3. 拆分两链表
23
+ cur = dummy = head.next
24
+ pre = head
25
+ while cur.next:
26
+ pre.next = pre.next.next
27
+ cur.next = cur.next.next
28
+ pre = pre.next
29
+ cur = cur.next
30
+ pre.next = None # 单独处理原链表尾节点
31
+ return dummy # 返回新链表头节点
32
+ ```
You can’t perform that action at this time.
0 commit comments