Special topic of linked list: Leetcode 19 delete the last N nodes of the linked list + Leetcode 24 exchange the nodes in the linked list in pairs + Leetcode 61 rotate the linked list

Posted by Calcartman on Sun, 09 Feb 2020 08:18:06 +0100

Special topic of linked list: Leetcode 19 delete the last N nodes of the linked list + Leetcode 24 exchange the nodes in the linked list in pairs + Leetcode 61 rotate the linked list

Leetcode 19 delete the penultimate node of the linked list

Title Description

Given a linked list, delete the nth last node of the list and return the head node of the list.

Example:

Given a list: 1 - > 2 - > 3 - > 4 - > 5, and n = 2
 When the penultimate node is deleted, the list changes to 1 - > 2 - > 3 - > 5

Explain:

The given n guarantee is valid.

Advance:

Can you try a scan implementation?

Title Solution

Since we want to pursue stimulation, we should carry it out to the end: directly meet this advanced request.

We need two nodes: = = one starting from the head node; one starting from the head node + N nodes. ==Then when the second node reaches the end, the next node of the head node is the location to be deleted. Then add the discussion of whether the deletion position is the boundary point to solve this problem easily:

The boundary points are as follows:

  1. When the deleted node is the head node
  2. The deleted node is the end node

The whole function is 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

Running time: 16 ms

Memory consumption: 11.7 MB

Leetcode 24 nodes in two exchange linked list

Title Description

Given a linked list, two pairs exchange the adjacent nodes and return the linked list after exchange.

You can't just change the internal values of nodes, but you need to actually exchange nodes.

Example:

Given 1 - > 2 - > 3 - > 4, you should return 2 - > 1 - > 4 - > 3

Title Solution

There is no special way, that is, to traverse from the beginning to the end and exchange the two. If there is only one node or there is no subsequent node, exit the traverse. 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

Leetcode 61 rotating list

Title Description

Given a list, rotate the list, and move each node of the list to the right by K positions, where k is a non negative number.

Example 1:

Input: 1 - > 2 - > 3 - > 4 - > 5 - > null, k = 2
 Output: 4 - > 5 - > 1 - > 2 - > 3 - > null
 Interpretation:
Rotate 1 step to the right: 5 - > 1 - > 2 - > 3 - > 4 - > null
 Rotate 2 steps to the right: 4 - > 5 - > 1 - > 2 - > 3 - > null

Example 2:

Input: 0 - > 1 - > 2 - > null, k = 4
 Output: 2 - > 0 - > 1 - > null
 Interpretation:
Rotate 1 step to the right: 2 - > 0 - > 1 - > null
 Rotate 2 steps to the right: 1 - > 2 - > 0 - > null
 Rotate 3 steps to the right: 0 - > 1 - > 2 - > null
 Rotate 4 steps to the right: 2 - > 0 - > 1 - > null

Title Solution

In fact, he doesn't have to be obedient. If he can find the length of the whole list, he only needs to move K mod len(list) k \ text {mod} len(list) k mod len(list).

First, connect the head and tail of the list to form a ring (the length of the chain can be calculated when connecting), then start from the node at the beginning, and go to the steps of len(list) - (k \ text {mod} len(list)) len(list) - (k \ text {mod} len(list)) len(list) - (k \ mod len(list)). This point is our new head node. Disconnect from here and output this node After the chain, so the previous has been connected to the end of the list.

The code is as follows:

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

Published 12 original articles, won praise 6, visited 583
Private letter follow