Leetcode OJ 143. Reorder List (two methods, fast and slow pointers, stacks)

Source: Internet
Author: User

Given a singly linked list l: l0→l1→ ... →ln-1→lN,
Reorder it to: l0→lnl1→ln-1→l2→l N-2→ ...

You must does this in-place without altering the nodes ' values.

For example,
Given {1,2,3,4} , reorder it to {1,4,2,3} .

Subscribe to see which companies asked this question

Answer:

The first method is to use the fast and slow pointer to find the middle node, and then reverse the second half of the linked list after the linked list, of course, it is important to note that the classification of the odd nodes and even the number of nodes, and note that the link list after the end of the new linked list to be assigned to NULL, otherwise the main function of the There is always to pay attention to the situation of empty table ...

/** Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode *next; *};*/voidReorderlist (structlistnode*head) {    structListNode *fast = head, *slow = head, *tmp, *tmp_head = NULL, *Tmp_tail;  while(NULL! = fast&&null! = fast->next) {Slow= slow->Next; Fast= fast->next->Next; }    if(NULL = =fast) {Tmp_tail=slow; }    Else{Tmp_tail= slow->Next; }     while(NULL! =tmp_tail) {tmp= tmp_tail->Next; Tmp_tail->next =Tmp_head; Tmp_head=Tmp_tail; Tmp_tail=tmp; } Slow=Tmp_head; if(NULL = =fast) {         while(NULL! =slow) {tmp= slow->Next; Slow->next = head->Next; if(NULL = =tmp) {Slow->next =NULL; } head->next =slow; Head= slow->Next; Slow=tmp; }    }    Else{         while(NULL! =slow) {tmp= slow->Next; Slow->next = head->Next; Head->next =slow; Head= slow->Next; Slow=tmp; } head->next =NULL; }}

The second method is to take advantage of the stack filo, so that the sequential traversal of the linked list of nodes into the stack, the node in order to pop up the list of links in sequence with the order of the requirements in the title, and pressed into the process of the stack also got the chain table length ... It is important to note that the value of the variable may change in the operation, and it is necessary to save the current value with a temporary variable ...

/** Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode *next; *};*/voidReorderlist (structlistnode*head) {    structlistnode* stack[100000]; structListNode *tmp =Head; inttop =-1, size, tmp_val;  while(NULL! =tmp) {stack[++top] =tmp; TMP= tmp->Next; } size= (top +1) /2; Tmp_val=top;  while(size) {size--; TMP=Stack[top]; Top--; TMP->next = head->Next; Head->next =tmp; Head= tmp->Next; }    if(0= = Tmp_val%2) {Head->next =NULL; }    Else if(NULL! =tmp) {tmp->next =NULL; }}

Leetcode OJ 143. Reorder List (two methods, fast and slow pointers, stacks)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.