代码随想录算法训练营第四天| 24. 两两交换链表中的节点、19. 删除链表的倒数第N个节点、 面试题 02.07. 链表相交、142.环形链表II

本文提供了四个关于链表操作的算法实现,包括两两交换链表中的节点,删除链表的倒数第N个节点,查找两个链表的相交节点,以及检测环形链表。这些方法通过迭代或双指针技术解决,展示了对链表数据结构的理解和操作技巧。
摘要由CSDN通过智能技术生成

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


代码思路

# 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]:
        if head == None:
            return None
        if head.next == None:
            return head
        n = 0
        pre_node = ListNode(val=-1, next=head)
        pre = pre_node
        cur = head
        while cur != None:
            if n % 2 == 0:
                if cur.next != None:
                    pre.next = cur.next
                pre = cur
                cur = cur.next
            else:
                next = cur.next
                cur.next = pre
                pre.next = next
                cur = next
            n += 1         
        return pre_node.next

19. 删除链表的倒数第N个节点


代码思路

该题是先构建节点的类,再在链表的类中调用节点类。

# 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]:
        count = 1
        pre_node = ListNode(-1, head)
        temp_head = pre_node
        temp_tail = head
        while count < n and temp_tail != None:
            temp_tail = temp_tail.next
            count += 1
        while temp_tail.next != None:
            temp_head = temp_head.next
            temp_tail = temp_tail.next
        temp_head.next = temp_head.next.next
        return pre_node.next


    def deleteAtIndex(self, index: int) -> None:
        pre = None
        cur = self.head
        if cur:
            if index == 0:
                self.head = self.head.next
                return None
            n = 0
            while n < index:
                if cur.next == None:
                    return None
    
                else:
                    pre = cur
                    cur = cur.next
                n += 1
            pre.next = cur.next

面试题 02.07. 链表相交


代码思路

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        curA, curB = headA, headB
        len_A = 1
        while curA != None:
            curA = curA.next
            len_A += 1
        len_B = 1
        while curB != None:
            curB = curB.next
            len_B += 1
        if len_A > len_B:
            longer, shorter = headA, headB
            diff = len_A - len_B
        else:
            shorter, longer = headA, headB
            diff = len_B - len_A
        longer_begin = longer
        count = 0
        while count < diff:
            longer_begin = longer_begin.next
            count += 1
        count = 0
        while longer_begin != None:
            if longer_begin != shorter:
                longer_begin = longer_begin.next
                shorter = shorter.next
            else:
                return longer_begin
            count += 1
        return None

142.环形链表II

代码思路

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        fast = head
        slow = None
        while fast != slow:
            if slow == None:
                slow = head
            if fast == None or fast.next == None:
                return None
            fast = fast.next.next
            slow = slow.next
        ptr = head

        while slow != ptr:
            slow = slow.next
            ptr = ptr.next

        return ptr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值