Microsoft interview 100 question series: answers to a combined linked list question [42nd Questions]

Source: Internet
Author: User

Microsoft interview 100 questions V0.1 42nd questions merged linked list answers

July, netizen January 2, 2011

------------------------------------

This article reference: I have compiled Microsoft interview 100 series V0.1 42nd Questions and Replies from netizens.
I declare that I am entitled to copyright to any materials in the 100 series such as Microsoft.

Because Microsoft and other 100 answer series for interviews V0.2, answers V0.3 [1-40 answer] have been released,
The answer version V0.3 has recently been sorted out. before uploading, You can selectively paste answers to several questions for readers to check.
As for the answers to questions 1-40, I will explain them in my blog occasionally.

OK, the answer to Question 56th [longest public subsequence] is already in my blog:
24 classic algorithm series: 3. Dynamic Planning Algorithm solutions are clearly stated in Microsoft interview question 56th.
This time, let's take a look at the interview questions of a linked list merger.

42. Modify the append function and use this function to achieve the union of two non-descending linked lists,
1-> 2-> 3 and 2-> 3-> 5 and 1-> 2-> 3-> 5. In addition, only results can be output, and data of two linked lists cannot be modified.

To merge a linked list, you must combine two non-ordered linked lists in sequence. As follows:
// Program 1: introduced by a netizen.
# Include <stdio. h>
# Include <malloc. h>

Typedef struct lnode {

Int data;
Struct lnode * next;
} Lnode, * linklist;

Linklist creatlist (int m) // create a linked list
{

Linklist p, l, s;
Int I;
P = l = (linklist) malloc (sizeof (lnode ));
P-> next = NULL;
Printf ("enter a number in the linked list :");
Scanf ("% d", & p-> data );
For (I = 2; I <= m; I ++)
{
S = (linklist) malloc (sizeof (lnode ));
S-> next = NULL;
Printf ("Enter the % d Number", I );
Scanf ("% d", & s-> data );
P-> next = s;
P = p-> next;
}
Printf ("\ n ");
Return l;
}

Void print (linklist h) // print the linked list
{
Linklist p = h-> next;
Int t = 1;
Printf ("print numbers: \ n ");
Do
{
Printf ("Please output the number of % d:", t );
Printf ("% d \ n", p-> data );
P = p-> next;
T ++;
} While (p );
}

Linklist mergelist (void) // merge two linked lists
{
Int e, n;
Linklist pa, pb, pc, head;
Printf ("Enter the length of the first linked list :");
Scanf ("% d", & e );
Pa = creatlist (e );
Printf ("Enter the length of the second linked list :");
Scanf ("% d", & n );
Pb = creatlist (n );
Head = pc = (linklist) malloc (sizeof (lnode ));
Pc-> next = NULL;
While (pa & pb)
{
If (pa-> data <= pb-> data)
{
Pc-> next = pa;
Pc = pa;
Pa = pa-> next;
}
Else
{
Pc-> next = pb;
Pc = pb;
Pb = pb-> next;
}
}
Pc-> next = pa? Pa: pb;
Return head;
}

Void main ()
{
Linklist head;
Head = mergelist ();
Print (head );
}

///////////////////////////////////
Enter the length of the first linked list: 5
Enter a number in the linked list: 3
Enter 2nd digits 2
Enter 3rd numbers 1
Enter 4th digits 7
Enter 5th digits 9

Enter the length of the second linked list: 5
Enter a number in the linked list: 6
Enter 2nd digits 4
Enter 3rd numbers 5
Enter 4th digits 8
Enter 5th digits 7

Print the numbers:
Output 1st count: 3
Output 2nd count: 2
Output 3rd count: 1
Please output 4th count: 6
Please output 5th count: 4
Output 6th count: 5
Please output 7th count: 7
Output 8th count: 8
Please output 9th count: 7
Please output 10th count: 9
Press any key to continue

// Procedure 2. Reference yangsen600.
# Include <stdio. h>
# Include <stdlib. h>
# Include <malloc. h>

Struct Node
{
Int num;
Node * next;
};

Node * createTail ()
{
Int x;
Node * head = NULL, * p = NULL, * tail = NULL;
Puts ("\ nplease enter some digits (end '.'):");
While (1 = scanf ("% d", & x ))
{
P = (Node *) malloc (sizeof (Node ));
P-> num = x;
P-> next = NULL;
If (NULL = head)
{
Tail = p;
Head = tail;
}
Else
{
Tail-> next = p;
Tail = p;
}
}
Getchar ();
Return head;
}

Node * CombinationNode (Node * head1, Node * head2)
{
Node * head, * tail, * p = head1, * q = head2, * s;

If (NULL = p)
Return q;
If (NULL = q)
Return p;

Tail = p;
If (p-> num> q-> num)
Tail = q;
Head = tail;

While (NULL! = P & NULL! = Q)
{
If (p-> num <= q-> num)
// If the element referred to by p <q indicates the element, the element referred to by p is first pulled into the merged linked list,
// P is assigned to s, and the next element p-> next of p is searched.
// Until it is found that p no longer refers to <q, but p> q, it is transferred to the else part of the following code.
{
S = p;
P = p-> next;
}
Else
{
S = q;
Q = q-> next;
}
Tail-> next = s;
Tail = s;
}

If (NULL = p)
P = q;
S = p;
Tail-> next = s;

Return head;
}

Void printHead (Node * head)
{
If (NULL = head)
Return;
Printf ("List :");
While (head)
{
Printf ("% d->", head-> num );
Head = head-> next;
}
Puts ("NUL ");
}

Void main (void)
{
Node * head1, * head2, * head;
Head1 = createTail ();
PrintHead (head1 );

Head2 = createTail ();
PrintHead (head2 );

Head = CombinationNode (head1, head2 );
PrintHead (head );
}

//////////////////////////////////////// //
Please enter some digits (end '.'):
3 2 1 7 9.
List: 3-> 2-> 1-> 7-> 9-> NUL

Please enter some digits (end '.'):
6 4 5 8 7.
List: 6-> 4-> 5-> 8-> 7-> NUL
List: 3-> 2-> 1-> 6-> 4-> 5-> 7-> 8-> 7-> 9-> NUL
Press any key to continue
// The output result is consistent with that in the preceding section.

 

42 form changes:
It is known that the two linked lists head1 and head2 are in an orderly order. Please combine them into a linked list.
// Procedure 3: Merge and sort linked lists using non-recursion:
Node * Merge (Node * head1, Node * head2)
{
If (head1 = NULL)
Return head2;
If (head2 = NULL)
Return head1;
Node * head = NULL;
Node * p1 = NULL;
Node * p2 = NULL;
If (head1-> data {
Head = head1;
P1 = head1-> next;
P2 = head2;
}
Else
{
Head = head2;
P2 = head2-> next;
P1 = head1;
}
Node * pcurrent = head;
While (p1! = NULL & p2! = NULL)
{
If (p1-> data <= p2-> data)
{
Pcurrent-> next = p1;
Pcurrent = p1;
P1 = p1-> next;
}
Else
{
Pcurrent-> next = p2;
Pcurrent = p2;
P2 = p2-> next;
}
}
If (p1! = NULL)
Pcurrent-> next = p1;
If (p2! = NULL)
Pcurrent-> next = p2;
Return head;
}

// Procedure 4. Recursive Implementation,
Node * MergeRecursive (Node * head1, Node * head2)
{
If (head1 = NULL)
Return head2;
If (head2 = NULL)
Return head1;
Node * head = NULL;
If (head1-> data {
Head = head1;
Head-> next = MergeRecursive (head1-> next, head2 );
}
Else
{
Head = head2;
Head-> next = MergeRecursive (head1, head2-> next );
}
Return head;
}

Bytes --------------------------------------------------------------------------------------------------
OK. Finally, let's thoroughly compare the same and different codes above.
If you do not want to make a comparison, the differences between program 1 and Program 2 regarding the core code of linked list merging are as follows:
Node * CombinationNode (Node * head1, Node * head2) // Program 2
{
Node * head, * tail, * p = head1, * q = head2, * s;

If (NULL = p)
Return q;
If (NULL = q)
Return p;

Tail = p;
If (p-> num> q-> num)
Tail = q;
Head = tail;

While (NULL! = P & NULL! = Q)
{
If (p-> num <= q-> num)
{
S = p; // 3.4
P = p-> next ;//
}
Else
{
S = q;
Q = q-> next;
}
Tail-> next = s;
Tail = s;
}

If (NULL = p)
P = q;
S = p;
Tail-> next = s;

Return head;
}

And this section:
Linklist mergelist (void) // program 1

{
Int e, n;
Linklist pa, pb, pc, head;
Printf ("Enter the length of the first linked list :");
Scanf ("% d", & e );
Pa = creatlist (e );
Printf ("Enter the length of the second linked list :");
Scanf ("% d", & n );
Pb = creatlist (n );
Head = pc = (linklist) malloc (sizeof (lnode); // 1. This
Pc-> next = NULL; // 2.
While (pa & pb)
{
If (pa-> data <= pb-> data)
{
Pc-> next = pa; // 3.
Pc = pa;
Pa = pa-> next;
}
Else
{
Pc-> next = pb; // 4.
Pc = pb;
Pb = pb-> next;
}
}
Pc-> next = pa? Pa: pb;
Return head;
}

Then compare the form differences between program 1 and program 4:
Linklist mergelist (void) // program 1
{
Int e, n;
Linklist pa, pb, pc, head;
Printf ("Enter the length of the first linked list :");
Scanf ("% d", & e );
Pa = creatlist (e );
Printf ("Enter the length of the second linked list :");
Scanf ("% d", & n );
Pb = creatlist (n );
Head = pc = (linklist) malloc (sizeof (lnode ));
Pc-> next = NULL;
While (pa & pb)
{
If (pa-> data <= pb-> data)
{
Pc-> next = pa; // 3
Pc = pa; // 1
Pa = pa-> next; // 2
}
Else
{
Pc-> next = pb;
Pc = pb;
Pb = pb-> next;
}
}
Pc-> next = pa? Pa: pb;
Return head;
}

And
// Recursive Implementation, Procedure 4
Node * MergeRecursive (Node * head1, Node * head2)
{
If (head1 = NULL)
Return head2;
If (head2 = NULL)
Return head1;
Node * head = NULL;
If (head1-> data {
Head = head1;
Head-> next = MergeRecursive (head1-> next, head2 );
}
Else
{
Head = head2;
Head-> next = MergeRecursive (head1, head2-> next );
}
Return head;
}

------------------------------

// The values 1, 2, and 3 marked by a program are equivalent to the values 1, 2, and 3 of the following procedure 4.
If (head1-> data {
Head = head1; // 1. head = head1;
Head-> next = MergeRecursive (head1-> next, head2); // 2. head1 = head1-> next;
// 3. head-> next = head1
}
Else
{
Head = head2;
Head-> next = MergeRecursive (head1, head2-> next );
}
Return head;
I believe you are smart. Don't explain it too much. :).

Author's statement:
My July has copyright to all content in this blog, reprinting or referencing any content or materials,
Indicate the author's July and source. Thank you.

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.