python指针

这篇博客探讨了三个与Python链表相关的操作:如何合并两个已排序的链表,如何成对交换链表中的节点,以及如何按指定位置旋转链表。每个操作都强调了使用dummy head的重要性,并提供了详细的步骤来解释解决问题的思路。

21. Merge Two Sorted Lists

Easy

142591276

Add to ListShare

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.

Example 1:

Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

Example 2:

Input: list1 = [], list2 = []
Output: []

Example 3:

Input: list1 = [], list2 = [0]
Output: [0]

Constraints:

  • The number of nodes in both lists is in the range [0, 50].
  • -100 <= Node.val <= 100
  • Both list1 and list2 are sorted in non-decreasing order.

这个题主要就是注意要加dummy head。有了dummy head, 在while的时候就可以让head的next从两个里面选一个,而不用从循环外单独选头结点

class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        #create dummy head
        head = new_list = ListNode(0); 
        
        while list1 and list2:
            if list1.val<list2.val:
                new_list.next = list1;
                list1 = list1.next;
            else:
                new_list.next = list2;
                list2 = list2.next;  
            new_list = new_list.next;
        new_list.next = list2 or list1;
            
        return head.next;
        

24. Swap Nodes in Pairs

Medium

7850324Add to ListShare

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)

Example 1:

Input: head = [1,2,3,4]
Output: [2,1,4,3]

Example 2:

Input: head = []
Output: []

Example 3:

Input: head = [1]
Output: [1]

Constraints:

  • The number of nodes in the list is in the range [0, 100].
  • 0 <= Node.val <= 100

Accepted

942.7K

Submissions

1.6M

交换相邻节点,首先要建一个头结点

2.存储第二个节点

3.把第一个节点的下一个指向第三个节点

4.把第二个节点的下一个指向第一个节点

5.把上一个节点的下一个指向新的第一个节点

6.移动当前指针到下一个节点

7.移动上一个节点指针到下一个节点

class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = p = ListNode(0)
        dummy.next = head
        while head and head.next:
            #store second node
            tmp = head.next
            #move first node's pointer to the 3rd node
            head.next = tmp.next
            #move second node's pointer to the first node
            tmp.next = head
            #move last head's pointer to the new first node
            p.next = tmp
            #move currrent pointer
            head = head.next
            #move the pointer to the last node
            p = tmp.next
        return dummy.next

 

 61. Rotate List

Medium

60581302Add to ListShare

Given the head of a linked list, rotate the list to the right by k places.

Example 1:

Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]

Example 2:

Input: head = [0,1,2], k = 4
Output: [2,0,1]

Constraints:

  • The number of nodes in the list is in the range [0, 500].
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 109

Accepted

615K

Submissions

1.7M

Seen this question in a real interview before?

Yes

No

Companies

Related Topics

Similar Questions

class Solution:
    def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        last = head
        if not head or not head.next:
            return head
        cur = head
        len = 1
        while cur.next:
            cur = cur.next
            len+=1
        k %= len
        while k:
            last = head
            #get the last element 
            
            while last.next.next:
                last = last.next
            #record the last element
            tmp = last.next
            last.next = None
            tmp.next = head
            head = tmp
            k-=1
        return head
                

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值