Interview Questions-linked list

Source: Internet
Author: User

How to prepare

Linked List questions are extremely common These can range from simple (delete a node ina linked list) to much more challenging either way, we advise you to be extremely comfortable with the easiest questions being able to easily manipulate a linked
List in the simplestways will make the tougher linked list questions much less tricky with that said, we present some "must know" code about linked list manipulation you shoshould be able to easily writethis code yourself prior to your interview

Linked lists are very common issues during interviews. The problem is difficult. Regardless of the question, we recommend that you familiarize yourself with the simple question first. Having mastered simple linked list questions, it is easier to face those challenges. So the following lists some essential code for linked list questions.

Creating a linked list:

Create a linked list:

Note: When you're discussing a linked list in an interview, make sure to understand whether it is a single linked list or a doubly linked list.
1 class node {2 node next = NULL; 3 int data; 4 Public node (INT d) {DATA = D;} 5 void appendtotail (int d) {6 node end = new node (d); 7 node n = This; 8 while (N. next! = NULL) {n = n. Next;} 9 N. Next = end; 10} 11}

Note: When writing a linked list during the interview, you must first know whether to write a single-chain table or double-chain table!

Deleting a node from a singly linked list

Delete a node from a single-chain table

1 node deletenode (node head, int d) {2 node n = head; 3 if (N. data = d) {4 return head. next;/* moved Head */5} 6 While (N. next! = NULL) {7 if (N. next. data = d) {8 n. next = n. next. next; 9 return head;/* head didn't change */10} 11 N = n. next; 12
} 13}

2 1 write code to remove duplicates from an unsorted linked list;
Follow up
How wocould you solve this problem if a temporary buffer is not allowed

2.1 Delete duplicate elements from an unordered linked list
Advanced: What should I do if I cannot use additional space?
2.1 Answer:
If a hash table can be used, check whether each element is repeated and remove it.

If no extra space is available, we need two pointers to traverse the array: Current for normal traversal, and runner for checking whether the elements are repeated. The runner only checks whether the element is repeated once, because each runner deletes all repeated elements of the element.

2.2 implement an algorithm to find the nth to last element of a singly linked list

2.2 The algorithm is used to find the nth element in a single-chain table.
Note: This issue strongly implies recursive algorithms, but here we use a more clever method. You must think more about similar problems. Can we use a non-recursive method instead of a recursive method.
2.2 answer:
Here we assume that the length of the linked list is at least N.
Algorithm Description:
(1) create two pointers, P1 and P2, pointing to the linked list header.
(2) Add n-1 times to P2 so that P2 points to the nth element from the head of the linked list (so that the distance between P1 and P2 is N)
(3) If P2-> next is empty, P1 is returned. If not empty, P1 and P2 are added at the same time. (If P2-> next is empty, it indicates that the element indicated by P1 is the nth to last, because the distance between P1 and P2 is n .)
(4) Repetition (3)

2.3 implement an algorithm to delete a node in the middle of a single linked list, given only access to that node
Example input: the node 'c' from the Linked List A-> B-> C-> D-> E result: Nothing is returned, but the new linked list looks like a-> B-> D-> E

2.3 implement an algorithm that only deletes an element (without a linked list header) from the linked list.
For example:
Input: node C (the original linked list is a-> B-> C-> D-> E)
Output: no return value. But the linked list changes to a-> B-> D-> E
2.3 answer:
The answer to this question is to copy the next input element to the input element to delete the input element. (Note: the original code does not release the space of the next element .)
Note: This method cannot delete the last element of the linked list. You need to clarify this with your interviewer. It doesn't matter if the algorithms are flawed. Tell your interviewer boldly that they like to see you propose these. You can discuss the solution with your interviewer.

2.4 you have two numbers represented by a linked list, where each node contains a single digit the digits are stored in reverse order, such that the 1's digit is at the head of the list write a function that adds the two numbers and returns
Sum as a linked list example input: (3-> 1-> 5) + (5-> 9-> 2) Output: 8-> 0-> 8

2.4 Two numbers represented by the linked list, with the cursor position in the header. Write a function to sum two such linked lists and return results in the same form.
For example: (3-> 1-> 5) + (5-> 9-> 2) returns 8-> 0-> 8
2.4 answer:
In this article, we can use recursive methods for addition by bit. Algorithm flow:

2

2 5 given a circular linked list, implement an algorithm which returns node at the begin-ning of the loop definition circular linked list: A (duplicate upt) linked list in which a node's next Pointer Points to an earlier node, so as to make a loop in
The linked list example input: A-> B-> C-> D-> E-> C [the same C as earlier] Output: c
Solution:
If we move two pointers, one with speed 1 and another with speed 2, they will end up meeting if the linked list has a loop why? Think about two cars driving on a track-the faster car will always pass the slower one! The tricky part here is finding the start
Of the loop imagine, as an analogy, two people racing around a track, one running twice as fast as the other if they start off at the same place, when will they next meet? They will next meet at the start of the next lap
Now, let's suppose fast runner had a head start of K meters on an n step lap when Will
They next meet? They will meet K meters before the start of the next lap (why? Fast runner wocould have made K + 2 (n-k) steps, including its head start, and slow runner wowould have made n-k steps both will be K steps before the start of the loop)
Now, going back to the problem, when fast runner (N2) and slow runner (N1) are moving around our circular linked list, n2 will have a head start on the loop when N1 enters specifically, it will have a head start of K, where k is the number of nodes before
The loop since N2 has a head start of K nodes, N1 and N2 will meet K nodes before the start of the loop
So, we now know the following:
1 Head is K nodes from loopstart (by definition)
2 meetingpoint for N1 and N2 is K nodes from loopstart (as shown above)
Thus, if we move N1 back to head and keep N2 at meetingpoint, and move them both at the same pace, they will meet at loopstart

2.5 to a linked list with a ring, design an algorithm to find the starting point of the ring.
Ring definition: The next element of an element in a linked list is a previous element.
For example, input a-> B-> C-> D-> E-> C to Output C
2.5 answer:
If there are two pointers in the linked list, P1 starts from the header and advances at the speed of one node at a time. P2 starts with 2 pointers at a time. Then these two pointers must meet in the ring of the linked list. Think about the two cars moving at different speeds on a ring runway, then they will certainly meet again on the runway.
The most difficult part of this problem is how to go beyond the nodes starting with this ring. Imagine where P2 would meet if it was on a circular runway at twice the speed of P1 starting from the starting point at the same time. Still at the starting point!
Now let's assume that if P2 starts from K meters, where will they meet for the first time? K meters behind the start line. (Why? If P2 encounters X and P1 after the starting line, P2 runs L-K + X, where L is the runway length, and P1 also runs X when it encounters. The equation (L-K-x)/2 = x/1 is obtained based on the time consumed during the encounter, and X = L-K is obtained, that is, the encounter at K points after the start line .)

Then return to the problem itself. When the P1 pointer just enters the ring, P2 leads P1 and points to K nodes in the ring. k indicates the distance from the head node of the linked list to the start of the ring. According to the previous conclusion, when P1 and P2.
The following conclusions are drawn:
(1) the header is located at K nodes from the beginning of the ring.
(2) P1 and P2 are also K nodes at the beginning of the ring.
If P1 and P2 meet each other, move P1 to the header and then the two pointers move at a speed of 1. Then the encounter is the beginning of the ring.

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.