[LeetCode] Reverse Linked List II, leetcodelinked

Source: Internet
Author: User

[LeetCode] Reverse Linked List II, leetcodelinked

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given1->2->3->4->5->NULL, M = 2 and n = 4,

Return1->4->3->2->5->NULL.

Note:
Given m, n satisfy the following condition:

1 ≤ m ≤ n ≤ length of list.

Idea: partially reverse the linked list and merge it.

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode *reverseBetween(ListNode *head, int m, int n) {        if(head == NULL || head->next == NULL)        {            return head;        }        ListNode *sg = new ListNode(-1);        sg->next = head;        head = sg;        ListNode *pre = head;        for(int i = 1; i < m; i++)        {            pre = pre->next;        }        ListNode *cur = pre->next;        ListNode *cur_next = cur->next;        if(n - m > 0)        {            int step = n - m;            while(step > 0 && cur_next != NULL)            {                ListNode *temp = cur_next->next;                cur_next->next = cur;                cur = cur_next;                cur_next = temp;                step--;            }            ListNode *temp = pre->next;            pre->next = cur;            temp->next = cur_next;        }        head = head->next;        delete sg;        return head;    }};



Sort a linked list in reverse order

Collections. reverse (Collections list );
The reverse Method in the Collections class can be sorted in reverse order!

Java recursive linked list whether the same element and different elements have two equal lists

What is this?
Not only is it costly, but it is also easy to make mistakes ......

Grouping sets first,
Private static boolean grouping sets (listing list list1, listing list list2 ){
Return subset (list1, list2) & subset (list2, list1); // the content of the set. The two sets are equal to each other's subsets.
}

Then difference,
Private static comment list difference (comment list list1, comment list list2 ){
While (isNull (list2 )){
If (isMember (first (list2), list1 ))
List1 = deleteAll (first (list2), list1); // remove duplicate elements
Else
List1 = add (first (list2), list1); // store non-repeating elements to list1
List2 = rest (list2 );
}
Return list1;
}

Last intersect,
Private static shortlist intersect (shortlist list1, shortlist list2 ){
List2 = difference (list1, list2); // get two sets of different elements
While (isNull (list2 )){
If (isMember (first (list2), list1 ))
; // The element is repeated and does nothing
Else
List1 = deleteAll (first (list2), list1); // Delete non-repeating Elements
List2 = rest (list2 );
}
Return list1;
}
 

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.