Data structure and algorithm - linked list (05)

Reverse a singly linked list.

Advanced:
Linked lists can be reversed iteratively or recursively. Can you do both?

Example :

Given this linked list:1->2->3->4->5

Return result: 5->4->3->2->1

topic link

Problem solving ideas:

1. Iterative version:

Circular list, define two pointers, one pointer is the last node of the linked list that has been iterated, called last_node, and one pointer is the first node of the node that has been iterated, called next_node.

At the beginning, both last_node and next_node point to the head of the linked list. The next node of the loop last_node is defined as cur, the next of last_node points to the next pointer of cur, and the next of cur points to the next_node node.

next_node is assigned the current cur node.

Finally, return to next_node.

The picture is as follows

1   ->2    ->3   ->4   ->5

|

next_node

last_node

After loop 1

2   ->1    ->3   ->4   ->5

|    |

|      last_node

next_node

code show as below:

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


class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None
        last_node = head
        next_node = head
        while (last_node.next):
            cur = last_node.next
            cur_next = cur.next
            cur.next = next_node
            last_node.next = cur_next
            next_node = cur
        return next_node

2. Recursive version

Recursion: Recursion is calling itself during the running process.

Conditions required to form recursion:
1. The sub-problem must be the same thing as the original problem and be simpler;
2. You cannot call itself indefinitely, there must be an exit, which is simplified to non-recursive state processing
First flip from the first point to flip from the next node until there is only one node left.
code show as below:
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if  head is None or head.next is None:
            return head
        pre_node = self.reverseList(head.next)
        head.next.next=head
        head.next=None

        return pre_node

 

Reverse Linked List II

Reverse the linked list from position  m  to  n  . Please use one scan to complete the reversal.

Explanation:
1 ≤  m  ≤  n  ≤ linked list length.

Example:

Input: 1->2->3->4->5->NULL, m = 2, n = 4

Output: 1->4->3->2->5->NULL

Problem solving ideas:

 You can first find the starting position of the flip, here is 2, and then break the linked list according to this position: 1->null, 2->3->4->5->null, which forms two linked lists.

Then, take the head node of the second linked list in turn and place it after 1:

1. 1->2->null, 3->4->5->null

2. 1->3->2->null, 4->5->null

。。。

How many times does this cycle repeat, 3 times, that is, n - m + 1 times

Then you can merge the two linked lists now. code is available

code show as below:

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

class Solution(object):
    def reverseBetween(self, head, m, n):
        """
        :type head: ListNode
        :type m: int
        :type n: int
        :rtype: ListNode
        """
        dummy = ListNode(-1)  
        dummy.next = head  
        pre = dummy  
        num = 1  
        # find the start of the section to flip  
        while num != m:  
            pre = pre.next  
            num += 1  
        # gap is the number of loops  
        gap = n - m + 1  
        # the second part  
        next_part = pre.next  
        # Set the tail node of the first part for the final merge  
        tail = next_part  
        pre.next = None  
        while gap != 0:  
            cur = next_part  
            next_part = next_part.next  
            temp = pre.next  
            pre.next = cur  
            cur.next = temp  
            gap -= 1  
        # merge the two parts  
        tail.next = next_part  
        return dummy.next    

  

 

Coding exchange group: 226704167, , Zhengzhou programmer group: 59236263 I would like to make progress with you!

WeChat public account:

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326893074&siteId=291194637