206 24 & recursion (list)

24

Given a linked list, swap every two adjacent nodes and return its head.

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

 

Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.

206

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?


 

Here we only consider a recursive algorithm, 24 are from the beginning, between two nodes in exchange. 206 is the reverse order from start to finish. (Considering only the switching node list or not the new node value exchange achieved)

We on the code

24

 1 class Solution {
 2 public:
 3     ListNode* swapPairs(ListNode* head) {
 4         if (head==NULL || head->next==NULL)
 5             return head;
 6         ListNode *t = head->next;
 7         head->next = swapPairs(t->next);
 8         t->next = head;
 9         return t;
10     }
11 };

206

 1 class Solution {
 2 public:
 3     ListNode* reverseList(ListNode* head) {
 4         if (head == NULL || head->next == NULL) {
 5             return head;
 6         }
 7         ListNode* ret = reverseList(head->next);
 8         head->next->next = head;
 9         head->next = NULL;
10         return ret;
11     }
12 };

Are 1-> 2-> 3-> 4 chain, for example:

(The number in the grid line number corresponding to the first code segment)

(* 3 represents the value of a pointer to the node 3)

(Double arrow line flow of the program)

 


 

24 when considering visible three node -> the current node, the next point to the current node, the next node the next node of the current node points (recursive return).

The first two nodes can exchange these three nodes, the nodes recursive return deemed to have the adjusted node. (Note that the recursive call in the middle of the code segment, this means that the central node pointer field modification)

And considering the 206, as is the entire exchange to list, then you have to have a macro concept, the ultimate return of the original list is the last node pointer, so constantly pushed pointer to the next node of the current node is executed, returns It is always "a pointer to the last node of the list relative to the original." (Note that the recursive call before the code segment, i.e., before the node pointer field modification, which enables direct pressure to the last node, the program continues to return a return value last recursion, the first time i.e. from the first unstack = * 4 <head unstack> is returned to the end of the same pointer)

Recursively occurred before and after the node point to a node itself, the node before the node is a front to itself do not edit pointer field, because the node to itself recursively returns the last recursive "next node of the node" a modifying pointer field points to the current node. After the end of the recursive return node pointer (now the head node)


 

Look procedures and written procedures are two different things, now glance at the number and position of the pointer others would be able to understand and write about. But entirely their own writing was too difficult.

1. What is returned

2. do

3. when to quit

-> 4 recursive calls position.

Each person's way to consider the issue, each option may have different causes. From the scale modeling of different issues, each option may do things different, might have a lot of road, but really limited access to the right path.

 

Guess you like

Origin www.cnblogs.com/katachi/p/12562278.html