-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample3.py
More file actions
50 lines (44 loc) · 1.09 KB
/
example3.py
File metadata and controls
50 lines (44 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
def listnodeJoin(list1 : ListNode, list2: ListNode) -> ListNode :
"""
递归调用把两个连接进行拼接,
:param list1:
:param list2:
:return:
"""
if not list1 :
return list2
elif not list2 :
return list1
if list1 and not list1.next :
list1.next = list2
return list1
result = listnodeJoin(list1.next, list2)
list1.next = result
return list1
def listnodeJoin_items(list1: ListNode, list2: ListNode) -> ListNode :
"""
当然迭代更容易理解
:param list1:
:param list2:
:return:
"""
if not list1 :
return list2
head = list1
while list1 and list1.next :
list1 = list1.next
list1.next = list2
return head
if __name__ == "__main__" :
node1 = ListNode(1)
node1.next = ListNode(2)
node1.next.next = ListNode(3)
node2 = ListNode(4)
node2.next = ListNode(5)
a = listnodeJoin_items(node1, node2)
print()