链表专题4 - leetcode61. Rotate List/143. Reorder List

61. Rotate List

题目描述

Given a linked list, rotate the list to the right by k places, where k is non-negative.

例子

Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL

Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL

解法

  • 第一次遍历,记录链表长度size
  • 有k%size个结点翻转到了前面

解法
记录长度,两次遍历 - (先把链表连成环)

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

class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if not head or not head.next:
            return head
        
        # Mark size and tail
        size = 1
        tail = head
        while tail.next:
            tail = tail.next
            size += 1
        tail.next = head
        
        # Rotate
        k = k % size
        p = head
        for _ in range(size-k-1):
            p = p.next
        node = p.next
        p.next = None
        return node    

143. Reorder List

题目描述

Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

You may not modify the values in the list’s nodes, only nodes itself may be changed.

例子
Example 1:

Given 1->2->3->4, reorder it to 1->4->2->3.

Example 2:

Given 1->2->3->4->5, reorder it to 1->5->2->4->3.

解法
法1 - split得到后半段(通过快慢指针)→反转后半段→merge
法2 - 建立辅助数组,存储所有结点后再操作

解法1
时间复杂度O(n),空间复杂度O(1)

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

class Solution(object):
    def reorderList(self, head):
        """
        :type head: ListNode
        :rtype: void Do not return anything, modify head in-place instead.
        """
        if not head or not head.next:
            return
        
        # Split - find the 2nd-half
        slow = fast = head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        node = slow.next
        slow.next = None
        
        # Reverse the 2nd-half
        node = self.reverseList(node) 
        
        # Merge head and node
        p = head
        while p.next:
            temp = node.next
            node.next = p.next
            p.next = node
            
            p = node.next
            node = temp
        p.next = node
     
    def reverseList(self, head):
        if not head or not head.next:
            return head
        node = self.reverseList(head.next)
        head.next.next = head
        head.next = None
        return node

解法2
时间复杂度O(n),空间复杂度O(n)

class Solution(object):
    def reorderList(self, head):
        """
        :type head: ListNode
        :rtype: void Do not return anything, modify head in-place instead.
        """
        arr = []
        while head:
            arr.append(head)
            head = head.next
        size = len(arr)
        for i in range(size//2):
            arr[i].next = arr[-i]
            arr[-i].next = arr[i+1]
        arr[size//2+1].next = None
            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码实现了`SuperInt`类的拷贝构造函数,用于将一个`SuperInt`对象复制到另一个`SuperInt`对象中。下面是代码的解释: 1. `this->list = new CellSuperInt;`创建一个新的链表,并将指针`list`指向新链表的头节点。 2. `this->list->next = nullptr;`将新链表的头节点的指针指向`nullptr`,表示新链表为空。 3. `this->list->value = s2.list->value;`将原链表的头节点的值复制到新链表的头节点中。 4. `CellSuperInt* p1 = this->list;`定义指针`p1`指向新链表的头节点。 5. `CellSuperInt* p2 = s2.list->next;`定义指针`p2`指向原链表的第一个节点。 6. `while (p2 != nullptr)`循环遍历原链表。 7. `CellSuperInt* newCell = new CellSuperInt;`创建一个新节点,并将指针`newCell`指向新节点。 8. `newCell->value = p2->value;`将原链表节点的值复制到新节点中。 9. `newCell->next = nullptr;`将新节点的指针指向`nullptr`,表示新节点为链表的最后一个节点。 10. `p1->next = newCell;`将新节点加入到新链表的末尾。 11. `p1 = p1->next;`将指针`p1`移动到新链表的末尾节点。 12. `p2 = p2->next;`将指针`p2`移动到原链表的下一个节点。 整个过程中,我们首先创建了一个新链表,并将原链表的头节点复制到新链表的头节点。然后遍历原链表,依次将每个节点复制到新链表中。最后得到了一个与原链表完全相同的新链表。 需要注意的是,在拷贝构造函数中,我们需要为新链表的每个节点都创建一个新的内存空间,否则新链表和原链表会共享同一个内存空间,从而导致在修改其中一个链表时,另一个链表也会被修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值