链表经典题目汇总
下面给出关于链表的经典题目,由易入难。多画图都很好理解!
1.leecode:反转链表
利用 p r e pre pre, c u r cur cur 这两个指针和 t e m p temp temp 这个临时指针,一次循环遍历即可。
循环解法:
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
pre = None
cur = head
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre
递归解法:
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if not head or not head.next:
return head
p = self.reverseList(head.next)
head.next.next = head #这一步是骚操作,使head和head。next之间的指针反向
head.next = None #这一步是尾结点的处理,因为是递归,所以假定前面已经反转完毕。
return p
- 反转位置m到位置n的链表,一次遍历。
leecode:反转指定位置链表
认真总结规律:
m
t
h
mth
mth 就是第
m
m
m 个节点,一直都是原始链表中第m个节点。
p
r
e
pre
pre 其实就是第
m
−
1
m-1
m−1 个节点
比如对于链表
1
−
>
2
−
>
3
−
>
4
−
>
5
−
>
N
u
l
l
1 ->2->3->4->5->Null
1−>2−>3−>4−>5−>Null,反转第二到第四个位置的链表。代码实现的过程如下:先找到第二个位置之前的节点
p
r
e
pre
pre,然后循环:
1
−
>
3
−
>
2
−
>
4
−
>
5
−
>
N
u
l
l
1 ->3->2->4->5->Null
1−>3−>2−>4−>5−>Null
1
−
>
4
−
>
3
−
>
2
−
>
5
−
>
N
u
l
l
1 ->4->3->2->5->Null
1−>4−>3−>2−>5−>Null
对照着例子,别嫌麻烦多看几遍就会懂的。
class Solution:
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
pre = ListNode(0)
pre.next = head
res = pre
count = 1
while count < m:
pre = pre.next
count += 1
mth = pre.next
while count < n:
cur = mth.next
mth.next =cur.next
cur.next = pre.next
pre.next = cur
count += 1
return res.next
这么写好像有些复杂,那我们其实可以参考第一个例子的反转链表的代码,将原始链表第
m
m
m 到第
n
n
n 的链表提取出来,反转之后再与之前的原始链表连接即可。
代码有详细的解释,看懂应该不难。不过着实也让自己吃了一惊,这次写这道题目的代码,两种方法基本都是一遍就过了,可见知识在于积累。多练习才是王道!
class Solution:
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
pre = ListNode(0)
pre.next = head
res = pre
count = 1
while count < m: #先找到第m个节点
pre = pre.next
count += 1
record1 = pre #记录第m-1个节点
for _ in range(n-m+1):
pre = pre.next
record2 = pre.next # 记录第n+1个节点
pre.next = None #将提取出来的链表尾结点指向None,然后反转
new = self.reverseList(record1.next)
record1.next = new #链接原始节点(第n-1个)
while new.next: # 遍历至反转后链表的最后一个非None尾结点
new = new.next
new.next = record2 #链接原始节点即可(第m+1个)
return res.next
def reverseList(self, head: ListNode) -> ListNode: #照搬第一个例子的代码
pre = None
cur = head
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre
思路也不麻烦,先遍历一次链表,得到链表长度(k),看给定的N是否超过链表长度(题目中给出的n是合理的,故没有判别)。如果N值合理,那么再遍历一次链表(k-n-1)即可得到答案。
这个是遍历两次的答案:
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
pre = ListNode(0)
pre.next = head
count = 0
while head:
head = head.next
count += 1
res = pre.next
if n == count:#删除首节点要单独讨论
return res.next
else:
for _ in range(count-n-1):#找到要删除节点的前一个节点
res = res.next
res.next = res.next.next #删除节点
return pre.next
遍历一次的答案:
设置快慢指针,快指针先走n步,然后快慢指针一起走,直到快指针的下一位为Null为止。由于快慢指针一直相差n个位置,此时的慢指针一定就是倒数第n+1个节点。
要额外考虑删除的节点是首节点,因此需要判断一下。
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
fast, slow,pre = head,head,None
while n > 1:
fast = fast.next
n -= 1
while fast.next:
pre = slow
fast = fast.next
slow = slow.next
if not pre:
return head.next
else:
pre.next = slow.next
return head
- 链表中的环(判断是否有环)
判断是否有环
快慢指针判定,如果有环,那么快慢指针必定有相遇的时候。如果没有环,循环会终止。
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if not head:
return False
slow, fast = head, head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast==slow:
return True
return False
链表中的环(输出入环的第一个节点)
入环的第一个节点
快慢指针一起走,先判定是否有环。无环则直接输出None,有环的时候有以下骚操作:
定义一个cur指针,指向head,然后和重合的快慢指针同时一步一步的走,当cur和快或慢指针相遇时,就是第一个入环点。
class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return None
slow, fast = head, head
find = False
while fast and fast.next and not find:
slow = slow.next
fast = fast.next.next
if slow == fast:
find = True
if find:
cur = head
while slow != cur:
slow = slow.next
cur = cur.next
return cur
else:
return None
- 奇偶链表
奇偶链表
画图,odd和even同时移动,注意判定条件就行。
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
if not head:
return
odd = head
even = head.next
evenhead = even
while even and even.next:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
odd.next = evenhead
return head
自己写了一遍,还是有bug,主要是想写好边界条件需要调试不同的链表。
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
if not head:
return
odd = ListNode(0)
even = ListNode(0)
ans = odd
evenhead = even
while head:
odd.next = head
odd = odd.next
head = head.next
even.next = head
even = even.next
if head:
head = head.next
if even:
even.next = None
odd.next = evenhead.next
return ans.next