Special list: N penultimate node to delete the list Leetcode 19 + Leetcode 24 twenty-two switching node in the linked list chain rotation + Leetcode 61

Special list: N penultimate node to delete the list Leetcode 19 + Leetcode 24 twenty-two switching node in the linked list chain rotation + Leetcode 61

Reciprocal Leetcode 19 to delete the list of N nodes

Title Description

Given a list, delete list reciprocal of n nodes, and returns to the head node list.

Example:

给定一个链表: 1->2->3->4->5, 和 n = 2.
当删除了倒数第二个节点后,链表变为 1->2->3->5.

Description:

N is given to ensure effective.

Advanced:

You can try to use one pass to achieve it?

answer

As to excitement, then we carry out in the end: to directly address this advanced request.

We need two nodes: == a departure from the head node; a node from scratch starting position + N nodes. == then when the second node reaches the end of time, under the head node of a node is to delete the location. Then add delete position is the point of discussing the border can easily solve this question:

There are a border point these types of situations:

  1. Node is removed when the head node
  2. Deleted node is the end node

Entire function like this:

class Solution(object):
    def removeNthFromEnd(self, head, n):
        front = head
        second = None
        count = 0
        flag = 0
        while front:
            if front.next:
                front = front.next
                count = count + 1
                if count > n-1:
                    if flag == 0:
                        second = head
                        flag = 1
                    else:
                        second = second.next
            else:
                break
        if second == None:
            res = head.next
            head = None
            return res
        else:
            if second.next:
                temp = second.next.next
                second.next = temp
                temp = None
            else:
                second.next = None
        return head

operation result

When run with: 16 ms

Memory consumption: 11.7 MB

Here Insert Picture Description

Leetcode 24 twenty-two switching node in the linked list

Title Description

Given a list pairwise exchange with adjacent nodes, and return the list to the exchange.

You can not just simply change the internal node values, but need to be the actual node exchange.

Example:

给定 1->2->3->4, 你应该返回 2->1->4->3.

answer

Not any particular method is to traverse back from the beginning again, between two to exchange, if no successor node after only one node or exit traversal. The whole process is as follows:

class Solution(object):
    def swapPairs(self, head):
        if not head:
            return head
        cur1 = head
        cur2 = head.next
        if not cur2:
            return head
        while True:
            if cur1 == head:
                cur1.next = cur2.next
                cur2.next = cur1
                head = cur2
            else:
                if not cur1.next:
                    break
                else:
                    if not cur1.next.next:
                        break
                    else:
                        temp1 = cur1.next
                        temp2 = cur1.next.next
                        temp1.next = temp2.next
                        cur1.next = temp2
                        temp2.next = temp1
                        cur1 = temp1
        return head

operation result

Here Insert Picture Description

Leetcode 61 rotating chain

Title Description

Given a list, the list of rotation, each of the node list to the right by k positions, wherein k is non-negative.

Example 1:

输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL
解释:
向右旋转 1 步: 5->1->2->3->4->NULL
向右旋转 2 步: 4->5->1->2->3->NULL

Example 2:

输入: 0->1->2->NULL, k = 4
输出: 2->0->1->NULL
解释:
向右旋转 1 步: 2->0->1->NULL
向右旋转 2 步: 1->2->0->NULL
向右旋转 3 步: 0->1->2->NULL
向右旋转 4 步: 2->0->1->NULL

answer

In fact, not so obedient, he said a few times to move move several times, if we can find the length of the entire list, just move k  mod  l e n ( l i s t ) a \ text {mod} len (list) on it.

First, the first connected end to end of the list, together into a ring (when the connection chain length can be determined), and then the head node starts walking l e n ( l i s t ) ( k  mod  l e n ( l i s t ) ) len (list) - (k \ text {mod} len (list)) step, the point is our new head node, here disconnected, the output of this node on it (as already chain, thus previously been connected to the end of the list a).

code show as below:

class Solution(object):
    def rotateRight(self, head, k):
        if (not head) | (k == 0):
            return head
        elif not head.next:
            return head
        cur = head
        length = 1
        while cur.next:
            cur = cur.next
            length = length + 1
        cur.next = head
        k = k % length
        if k == 0:
            cur.next = None
            return head
        cur = head
        for i in range(1, length - k):
            cur = cur.next
        head = cur.next
        cur.next = None
        return head

operation result

Execution Time: 24 ms

Memory consumption: 11.9 MB

Here Insert Picture Description

Published 12 original articles · won praise 6 · views 592

Guess you like

Origin blog.csdn.net/weixin_44618103/article/details/104234987