Algorithm pen question 2-java

Source: Internet
Author: User

(1) Flip chain list

List node definition: public class Listnode<t> {    T val;    ListNode Next;}
The key to flipping the list is to handle the intermediate state. The middle state is that some of the linked lists have been flipped (denoted by head), part of the list has not been flipped (denoted by next), and the node pointed by next is added to the node that the head points to.
And head moves to the next position, next moves to the Next.next position, and then back to the middle state. Iterate until next is null, at which point the list is all flipped and head is pointing to the header of the flipped list.
 PublicListNode Reverse (listnode head) {if(Head = =Null | | head.next = = NULL) {        returnHead; } ListNode Next=Head.next; Head.next=NULL;  while(Next! =NULL) {ListNode tmp1=Head; Head=Next; ListNode TMP2=Next.next; Next.next=TMP1; Next=TMP2; }    returnhead;}

(2) Small easy to have a positive integer sequence of length N = {A[1], a[2], a[3] ..., A[n]}.
Dr. Niu gave a little difficulty:
The logarithmic column A is rearranged so that the sequence a satisfies all A[i] * a[i + 1] (1≤i≤n-1) is a multiple of 4.
Small easy now need to determine whether a sequence can be re-ordered to meet the requirements of Dr. Niu.

Input Description:
Enter the number of first behavior series T (1≤T≤10), next each two lines describe a sequence A, the first behavior sequence length n (1≤n≤10^5) second act n Positive integer A[i] (1≤a[i]≤10^9)
Output Description:
For each column of the output line indicates whether to meet the requirements of Dr. Niu, if you can output yes, otherwise output No.
Input Example 1:
231 10 10041 2 3 4
Output Example 1:
YesNo
/**
The key is to determine that all adjacent two multiplication can be divisible by 4, the number of t_4 can be divisible by 4, can be divisible by 2 can not be divisible by 4 of the number of t_2, the other number is t_1, you can be divisible by 4 needs to meet the following conditions:
(1) t_2>0, T_4>=T1, at this time all the t_2 number of rows on the left, t_4/t_1 interval on the right, t_4/t_1 number does not exist or affect
(2) T_2=0, T_4 must have, then t_4>=1, the t_4 into all t_1 number in the middle of the need for the least number of t_4, at this time t_4=t_1-1, so the number of T_4>=t1-1,t_4 no impact
*/
ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); intt =Scanner.nextint (); for(inti = 0; I < T; i++) { intCount =Scanner.nextint (); int[] arr =New int[Count]; for(intj = 0; J < Count; J + +) {Arr[j]=Scanner.nextint (); } intT_4 = 0, t_2 = 0, t_1 = 0; for(intNum:arr) { intn =num; if(n% 4 = = 0) {T_4++; } Else if(n% 2 = = 0) {t_2++; } Else{t_1++; } } Booleancan =false; if(T_2 > 0) {can= T_4 >=t_1; } Else{can= t_4 > 0 && (t_4 >= t_1-1); } System.out.println (Can? "Yes": "No"); } }}

(3) Merging two lists with good order

/*Public class ListNode {int val;    ListNode next = null;    ListNode (int val) {this.val = val; }}*/ Public classMain { PublicListNode Merge (listnode list1,listnode list2) {if(List1 = =NULL) {            returnList2; }        if(List2 = =NULL) {            returnList1; } ListNode head=NULL, iter =NULL;
Determining the head elementif(List1.val <list2.val) {head= ITER =List1; List1=List1.next; } Else{Head= ITER =List2; List2=List2.next; } while(List1! =NULL&& List2! =NULL) { if(List1.val <list2.val) {iter= Iter.next =List1; List1=List1.next; } Else{iter= Iter.next =List2; List2=List2.next; } } while(List1! =NULL) {iter= Iter.next =List1; List1=List1.next; } while(List2! =NULL) {iter= Iter.next =List2; List2=List2.next; } returnHead; }}

Algorithm pen question 2-java

Related Article

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.