【代码随想录|链表 24. 两辆交换链表中的节点,19.删除链表的倒数第n个节点】

代码随想录|链表 24. 两辆交换链表中的节点,19.删除链表的倒数第n个节点


python

一、24. 两辆交换链表中的节点

24. 两辆交换链表中的节点

1.核心代码

代码如下(示例):

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(0)
        dummy.next=head
        pre = dummy
        cur=pre.next
        while pre.next and cur.next:
            pre.next=pre.next.next
            cur.next=cur.next.next
            pre.next.next=cur
            pre=pre.next.next
            cur=pre.next
        return dummy.next

2.输入输出

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next

def swapPairs(head: ListNode) -> ListNode:
    dummy = ListNode(0)
    dummy.next = head
    pre = dummy
    cur = pre.next
    while pre.next and cur.next:
        pre.next = pre.next.next
        cur.next = cur.next.next
        pre.next.next = cur
        pre = pre.next.next
        cur = pre.next
    return dummy.next

def create_list(nums):
    dummy = ListNode(0)
    cur=dummy
    for num in nums:
        cur.next=ListNode(num)
        cur=cur.next
    return dummy.next

def list_to_array(head):
    arr=[]
    cur=head
    while cur:
        arr.append(cur.val)
        cur=cur.next
    return arr

if __name__=="__main__":
    # 用户输入
    input_str = input("输入,形式为head=[1,2,3,4]:")
    # 将输入字符串转化为列表
    input_str = input_str[input_str.find('[')+1:input_str.find(']')]
    input_num = list(map(int,input_str.strip().split(',')))
    # 将列表转换为链表
    head = create_list(input_num)
    # 核心功能
    swap_head = swapPairs(head)
    # 输出,链表变列表
    print("输出",list_to_array(swap_head))

每种类别的输入输出只要掌握一个就全部掌握了,所以非常简单,第二次写就会了,主要是字符串到列表到链表的转化,然后列表到链表,和链表到列表要再写两个函数

二、19.删除链表的倒数第n个节点

1、核心代码

核心代码完全没有问题,链表这块已经基本掌握了

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        dummy = ListNode(0)
        dummy.next=head
        pre = dummy
        cur=dummy
        
        while n:
            pre=pre.next
            n-=1
        while pre.next:
            pre=pre.next
            cur=cur.next
        
        temp=cur.next.next
        cur.next=cur.next.next
        return dummy.next
        

2、输入输出

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next

def removeNthFromEnd( head: ListNode, n: int) -> ListNode:
    dummy = ListNode(0)
    dummy.next = head
    pre = dummy
    cur = dummy
    while n:
        pre = pre.next
        n -= 1
    while pre.next:
        pre = pre.next
        cur = cur.next
    cur.next = cur.next.next
    return dummy.next

def create_list(nums):
    dummy =ListNode(0)
    cur=dummy
    for num in nums:
        cur.next=ListNode(num)
        cur=cur.next
    return dummy.next

def list_to_array(head):
    arr=[]
    while head:
        arr.append(head.val)
        head=head.next
    return arr


if __name__=="__main__":
    input_str=input("输入:")
    input_str=input_str[input_str.find('[')+1:input_str.find(']')]
    input_nums=list(map(int,input_str.strip().split(',')))
    input_ns=input("输入n:")
    input_ns=input_ns.split('=')[1].strip()
    n=int(input_ns)
    head=create_list(input_nums)
    new_head=removeNthFromEnd(head,n)
    print("输出",list_to_array(new_head))

输入输出有一点小报错,
1、不会取字符串的end,给出的解决方案:直接用等号分割,并取其第二个

input_ns=input_ns.split('=')[1].strip()

2、不用到虚拟节点的地方可以直接从head开始
3、要用虚拟结点的地方,注意除了虚拟结点,还要取一个当前的指针

总结

输入输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值