链表

链表初始化
class ListNode:
def __init__(self, val=0):
self.val = val
self.next = None
1
2
3
4
class ListNode:
def __init__(self, val=0):
self.val = val
self.next = None
self.prev = None
1
2
3
4
5
2. 两数相加
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
pass
1
2
3
4
5
6
7
8
9
10
19. 删除链表的倒数第 N 个结点
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
dummy = ListNode(0)
dummy.next = head
x = self.findFromEnd(dummy, n + 1)
x.next = x.next.next
return dummy.next
def findFromEnd(self, head: ListNode, k: int):
p1 = head
for i in range(k):
p1 = p1.next
p2 = head
while p1:
p2 = p2.next
p1 = p1.next
return p2
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
21. 合并两个有序链表
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def mergeTwoLists(self, list1: ListNode, list2: ListNode):
if list1 is None or list2 is None:
return list2 if list1 is None else list1
head = ListNode(0)
if list1.val <= list2.val:
head = list1
else:
head = list2
cur1 = head.next
if head == list1:
cur2 = list2
else:
cur2 = list1
pre = head
while cur1 and cur2:
if cur1.val <= cur2.val:
pre.next = cur1
cur1 = cur1.next
else:
pre.next = cur2
cur2 = cur2.next
pre = pre.next
if cur1:
pre.next = cur1
else:
pre.next = cur2
return head
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
24. 两两交换链表中的节点
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
if not head or not head.next:
return head
new_head = head.next
head.next = self.swapPairs(new_head.next)
new_head.next = head
return new_head
def swapPairs2(self, head: ListNode) -> ListNode:
res = ListNode(next=head)
pre = res
while pre.next and pre.next.next:
cur = pre.next
post = pre.next.next
cur.next = post.next
post.next = cur
pre.next = post
pre = pre.next.next
return res.next
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
25. K 个一组翻转链表
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reverseKGroup(self, head: ListNode, k: int):
start = head
end = self.get_group_end(start, k)
if end is None:
return head
head = end
self.reverse(start, end)
last_end = start
while last_end.next != None:
start = last_end.next
end = self.get_group_end(start, k)
if end is None:
return head
self.reverse(start, end)
last_end.next = end
last_end = start
return head
def reverse(self, start: ListNode, end: ListNode):
end = end.next
pre = None
cur = start
next = None
while cur != end:
next = cur.next
cur.next = pre
pre = cur
cur = next
start.next = end
def get_group_end(self, start: ListNode, k: int):
while k != 0 and start:
start = start.next
k = k - 1
return start
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
61. 旋转链表
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if k == 0 or not head or not head.next:
return head
n = 1
cur = head
while cur.next:
cur = cur.next
n += 1
cut = n - k % n
cur.next = head
while cut:
cut -= 1
cur = cur.next
new_head = cur.next
cur.next = None
return new_head
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
82. 删除排序链表中的重复元素 II
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head:
return head
dummy = ListNode(0, head)
pre = dummy
cur = head
while cur:
while cur.next and cur.val == cur.next.val:
cur = cur.next
if pre.next == cur:
pre = pre.next
else:
pre.next = cur.next
cur = cur.next
return dummy.next
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
83. 删除排序链表中的重复元素
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head:
return head
cur = head
while cur.next:
if cur.val == cur.next.val:
cur.next = cur.next.next
else:
cur = cur.next
return head
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
86. 分隔链表
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
small = ListNode(0)
large = ListNode(0)
small_head = small
large_head = large
while head:
if head.val < x:
small.next = head
small = small.next
else:
large.next = head
large = large.next
head = head.next
large.next = None
small.next = large_head.next
return small_head.next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
92. 反转链表 II
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
def reverse(head: ListNode):
pre = None
cur = head
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
dummy = ListNode(0)
dummy.next = head
pre = dummy
for _ in range(left - 1):
pre = pre.next
right_node = pre
for _ in range(right - left + 1):
right_node = right_node.next
left_node = pre.next
cur = right_node.next
pre.next = None
right_node.next = None
reverse(left_node)
pre.next = right_node
left_node.next = cur
return dummy.next
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
141. 环形链表
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def hasCycle(self, head: ListNode) -> bool:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
def hasCycle2(self, head: ListNode) -> bool:
visted = set()
while head:
if head in visted:
return True
visted.add(head)
head = head.next
return False
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
142. 环形链表 II
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
break
if fast is None or fast.next is None:
return None
slow = head
while slow != fast:
slow = slow.next
fast = fast.next
return slow
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
147. 对链表进行插入排序
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
if not head:
return head
dummy = ListNode(0)
dummy.next = head
last_sorted = head
cur = head.next
while cur:
if last_sorted.val <= cur.val:
last_sorted = last_sorted.next
else:
prev = dummy
while prev.next.val <= cur.val:
prev = prev.next
last_sorted.next = cur.next
cur.next = prev.next
prev.next = cur
cur = last_sorted.next
return dummy.next
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
148. 排序链表
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def sortList(self, head: ListNode) -> ListNode:
pass
1
2
3
4
5
6
7
8
9
10
160. 相交链表
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
if not headA or not headB:
return None
pa = headA
pb = headB
while pa != pb:
if not pa:
pa = headB
else:
pa = pa.next
if not pb:
pb = headA
else:
pb = pb.next
return pa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
203. 移除链表元素
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
dummy_head = ListNode(0, next=head)
cur = dummy_head
while cur.next:
if cur.next.val == val:
cur.next = cur.next.next
else:
cur = cur.next
return dummy_head.next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
206. 反转链表
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
pre = None
cur = None
while head:
cur = head.next
head.next = pre
pre = head
head = cur
return pre
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
234. 回文链表
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
stack = []
cur = head
while cur:
stack.append(cur)
cur = cur.next
while stack:
xxx = stack.pop()
if xxx.val != head.val:
return False
head = head.next
return True
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
328. 奇偶链表
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
if not head or not head.next or not head.next.next:
return head
even_head = head.next
odd = head
even = even_head
while even and even.next:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
odd.next = even_head
return head
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
707. 设计链表
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class MyLinkedList:
def __init__(self):
self.head = ListNode(0)
self.size = 0
def get(self, index: int) -> int:
if index < 0 or index >= self.size:
return -1
cur = self.head
for _ in range(index + 1):
cur = cur.next
return cur.val
def addAtHead(self, val: int) -> None:
new_node = ListNode(val)
new_node.next = self.head.next
self.head.next = new_node
self.size += 1
def addAtTail(self, val: int) -> None:
new_node = ListNode(val)
cur = self.head
while cur.next:
cur = cur.next
cur.next = new_node
self.size += 1
def addAtIndex(self, index: int, val: int) -> None:
if index < 0:
self.addAtHead(val)
return
elif index == self.size:
self.addAtTail(val)
return
elif index > self.size:
return
new_node = ListNode(val)
pre = self.head
while index:
pre = pre.next
index -= 1
new_node.next = pre.next
pre.next = new_node
self.size += 1
def deleteAtIndex(self, index: int) -> None:
if index < 0 or index >= self.size:
return
pre = self.head
while index:
pre = pre.next
index -= 1
pre.next = pre.next.next
self.size -= 1
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
876. 链表的中间结点
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
链表相交
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
if not headA or not headB:
return None
pa = headA
pb = headB
while pa != pb:
if not pa:
pa = headB
else:
pa = pa.next
if not pb:
pb = headA
else:
pb = pb.next
return pa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
剑指 Offer 22. 链表中倒数第 k 个节点
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
pass
1
2
3
4
5
6
7
8
9
10