LeetCode 链表(19,21,23,24)

#NO.19 删除链表的倒数第N个节点
“”"

示例:

给定一个链表: 1->2->3->4->5, 和 n = 2.

当删除了倒数第二个节点后,链表变为 1->2->3->5.

“”"
def remove_nth_fomr_end(head, n):
tail = head
previous = head
prior = head
i = 1
while i < n and tail.next:
tail = tail.next
i += 1
if i == n:
while tail.next:
prior = previous
previous = previous.next
tail = tail.next
print(id(head), id(prior), id(previous), id(tail))
if head is previous:
head = previous.next
else:
prior.next = previous.next
return head
“”"
方法一:感觉思路清晰,但是代码有冗余。不够简洁。
“”"
“”"
以下是方法二:

“”"
def remove_nth_fomr_end(head, n):
first = second = head
for _ in range(n): #给定n确认正确,直接遍历即可。
first = first.next
if not first: #倒数第一个
return head.next
while first.next: #存在时,用双next表示空一个。删除一个。
first = first.next
second = second.next
second.next = second.next.next
return head

“”"
21. 合并两个有序链表

1->2->4, 1->3->4
输出:1->1->2->3->4->4
“”"""

class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
head = dummy = ListNode(-1) #注意这个写法。直接用.next取ListNode(0)
while l1 and l2:
if l1.val < l2.val:
head.next = l1
l1 = l1.next
else:
head.next = l2
l2 = l2.next
head = head.next
if l1:
head.next = l1
if l2:
head.next = l2
return dummy.next
#方法二:

class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if not l1 or not l2: #用or判断简写
return l1 or l2
if l1.val < l2.val: #判断然后用迭代的方式,一层一层的加。
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
else:
l2.next = self.mergeTwoLists(l1, l2.next)
return l2

“”"
23. 合并K个排序链表

输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6

相当于21的升级版
“”"""

#第一种方法:总体是思路是借用21题两个链表排序方式,用递归的方式两两和并。
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
amount = len(lists)
interval = 1
while interval < amount:
for i in range(0, amount - interval, interval * 2):
lists[i] = self.merge2Lists(lists[i], lists[i + interval])
interval *= 2
return lists[0] if amount > 0 else lists
def merge2Lists(self,l1,l2):
head = point = ListNode(0)
while l1 and l2:
if l1.val <= l2.val:
point.next = l1
l1 = l1.next
else:
point.next = l2
l2 = l2.next
point = point.next
if not l1:
point.next = l2
else:
point.next = l1
return head.next

#第二种方式: 列表放入遍历出的排序后,转成链表

class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
self.nodes = []
head = point = listNode(0)
for l in lists:
while l:
self.nodes.append(l.val)
l = l.next
for x in sorted(self.nodes):
point.next = ListNode(x)
point = point.next
return head.next

“”"
24. 两两交换链表中的节点
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例:
给定 1->2->3->4, 你应该返回 2->1->4->3.

“”"
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
pre, pre.next = self, head
while pre.next and pre. next. next:
a= pre. next
b= a. next
pre.next, b. next, a.next= b, a, b.next
pre= a
return self.next

#二种写法其实思路差不多。就是对比学习一下。表达方式。
#相对来说第二种比较好理解。

class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
cur = dummy = ListNode(0)
cur.next = head
while cur.next and cur.next.next:
cur.next.next.next,cur.next.next,cur.next,cur =
cur.next,cur.next.next.next,cur.next.next,cur.next
return dummy.next

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值