链表(1-10题)回顾
10. 链表元素按奇偶聚集(Medium)
leedcode: 328. Odd Even Linked List (Medium)
描述:
Given 1->2->3->4->5->NULL,
return 1->3->5->2->4->NULL.
代码:
class Solution(object):
def oddEvenList(self, head):
if not head:
return None
odd = head
even = head.next
evenhead = even
while even and even.next:
odd.next = odd.next.next
odd = odd.next
even.next = even.next.next
even = even.next
odd.next = evenhead //此处odd已移动至奇数项最尾部,按题意,奇数项后面
//应紧跟偶数项,故有:odd.next = evenhead
return head
【注意】
1,该题中,head作为标签,始终指向链表头部,而odd与even作为移动标签,则在改变着原始列表的排序顺序。故而最终返回head,即可输出已经调过顺序的链表。
2,odd = odd.next
表示指针odd
移动到了odd.next
odd.next = odd
表示指针odd
指向自己
‘’’
‘’’
9. 分隔链表(Medium)
leedcode: 725. Split Linked List in Parts(Medium)
描述:
Input: root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
输入的链表被分成若干连续子链表,大小链表元素之差最多为1,且靠前子表比靠后的大。
代码:
class Solution(object):
def splitListToParts(self, root, k):
head = root
length = 0
##### 计算链表总长度
while head:
head = head.next
length += 1
#### 设定新的总链表 l1
size, longer = length//k, length%k
l1 = [size+1]*longer+[size]*(k-longer)
#### 将原链表中的元素按序填入新的链表 l1 中,并最终返回
head = root
prev = None
for index, num in enumerate(l1):
l1[index] = head
if prev:
prev.next = None
for i in range(num):
prev = head
head = head.next
return l1
【算法难点】
1,l1 = [size+1]*longer+[size]*(k-longer)
: 在Python 中,这表示列表,如
l1 = [3]*2+[2]*1
就表示l1 = [3,3,2]
2,for index, num in enumerate(l1)
: 在Python 中,enumerate()
是一个内置函数,为枚举函数;以此题为例,index
表示链表 l1 中各个元素的位置,num
表示链表 l1 中各个元素,即
index = 0,1,2
num = 3,3,2
3, for i in range(3)
:表示一共循环三次
‘’’
‘’’
8. 回文链表(Easy)
leedcode: 234. Palindrome Linked List (Easy)
描述
Example 1:
Input: 1->2
Output: false
Example 2:
Input: 1->2->2->1
Output: true
想法:用两个指针,把链表表分为前后两个依次比较
代码
class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
rev = None
fast = slow = head
while fast and fast.next:
fast = fast.next.next
rev, rev.next, slow = slow, rev, slow.next
if fast:
slow = slow.next
while rev and rev.val == slow.val:
slow = slow.next
rev = rev.next
return not rev
【算法难点】
本题涉及到了python 语言下列表元素的反转,应掌握
rev = None
root = head
while (//条件):
rev, rev.next, slow = slow, rev, slow.next
【注意】
python 中要注意单个赋值与连续赋值的区别
可参考关于python连续赋值与Python连续赋值需要注意的地方
‘’’
‘’’
7. 链表求和
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
代码:
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
""
carry = 0
root = cur = ListNode(0)
while l1 or l2 or carry:
if l1:
carry += l1.val
l1 = l1.next
if l2:
carry += l2.val
l2 = l2.next
cur.next = ListNode(carry%10)
cur = cur.next
‘’’
‘’’
5、1双指针的例子(fast 与 slow 两个指针)
1. 找出两个链表的交点(easy)
160. Intersection of Two Linked Lists (Easy)
描述
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
5. 删除链表的倒数第 n 个节点(medium)
19. Remove Nth Node From End of List (Medium)
描述
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end,
the linked list becomes 1->2->3->5.
2. 链表反转(务必掌握)
206. Reverse Linked List (Easy)
描述
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
代码
方法一
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
prev = None
while head:
prev, prev.next, head = head, prev, head.next
return prev
方法二
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
newHead = None
while (head != null)
next = head.next
head.next = newHead.next
newHead.next = head;
head = next
return newHead.next