Java code implementation of simple consumer list:
public class LinkedListNode {public int value;public LinkedListNode next;public LinkedListNode(int value) {this.value = value;this.next = null;}}
public class LinkedList {public LinkedListNode head;/*add an element to the tail*/public void add(int v) {if (head == null) {this.head = new LinkedListNode(v);} else {LinkedListNode node = head;while (node.next != null) {node = node.next;}node.next = new LinkedListNode(v);}}/*remove the element that appear first*/public void removeFirst(int v) {/* if to be removed is head node */if (head.value == v) {head = head.next;return;}/* iterate to the end and remove target node */LinkedListNode node = head;while (node.next != null) {if (node.next.value == v) {node.next = node.next.next;return;}node = node.next;}}public void print() {LinkedListNode node = head;while (node != null) {System.out.print(node.value + " ");node = node.next;}System.out.println();}}
1. Write Code to remove duplicated element from an unsorted linked list. Remove duplicate elements from unordered linked lists.
Source cracking the code interview (2nd edition) CH2 Q1.
[Answer]: The question has clearly stated that it is an unordered list. For such interview questions, the worst complexity is O (nlogn), that is, a sorting process is required, therefore, it is better to give an answer than O (nlogn ).
Method 1: In addition to repeated elements, hash is used first. Iterate the list, put an element into hashtable, and determine whether it is repeated. If it is repeated, delete the element from the list. The time complexity of this algorithm is O (n), but it requires O (n) space to create hashtable to store elements.
Java code: http://pastebin.com/UiqMVxdL here the listing list uses the listing list in Java. util. In actual interviews, you often need to implement an external list by yourself.
public static void removeDuplicate(List<Integer> l){2. Hashtable table = new Hashtable();3. for(Iterator<Integer> iter = l.iterator();iter.hasNext();){4. Integer i = iter.next();5. if(table.containsKey(i)){6. iter.remove();7. }else{8. table.put(i, true);9. }10. }11. }
Method 2:ExploitationRunner pointer. This technique is also one of the important skills to answer the problem list problem. Iterates the current linked list. Each iteration uses a runner to access the elements and deletes the repeated elements. It can be regarded as naive approach, time complexity O (N ^ 2), and space complexity O (1 ). This answer is the most likely to come to mind in the interview, and then the first method can be optimized.
2. Implement am algorithm to find the kth to last element of a singly linked list. Find the elements in a single necklace table whose distance from the last element is K.
Source: cracking the code interview (2nd edition) CH2 q2.
[Answer ]:Method 1:If you know the length N of the linked list, the answer is from the second (N-K) element. Of course, this question is meaningless.
Method 2: Use chaser-runner.First, let the runner move the K-bit first. At this time, the chaser starts from the head of the linked list and iterates the linked list at the same speed as the runner. When the runner reaches the end of the linked list, the Chaser is exactly the distance from the last element K. Time complexity O (N) and space complexity O (1 ).
Java code: The http://pastebin.com/MQczDMvT used here is the data structure defined at the beginning of this article.
public static LinkedListNode kth2Last(LinkedList list, int k) {2. LinkedListNode chaser = list.head;3. LinkedListNode runner = list.head;4. 5. int i = 0;6. 7. while (i < k) {8. /*the list must have at least k+1 elements*/9. if (runner == null) {10. return null;11. }12. runner = runner.next;13. i++;14. }15. 16. /*if the list has exactly k elements*/17. if(runner == null){18. return null;19. }20. 21. 22. /*begin chasing*/23. while(runner.next!=null){24. chaser = chaser.next;25. runner = runner.next;26. }27. return chaser;28. }
3. Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop. Find the header with the Link Ring.
Source cracking the code interview (2nd edition) CH2 q6.
[Answer]: This is one of the typical interview questions. Like binary tree's BFS traversal, it seems to be full of levels, and there are not many difficult questions in reality, what if I lose my personal character if I encounter it !?
First, the first question is generally to judge whether the linked list has a ring, and then ask if there is a ring, find the Start Node of the ring.
If a ring exists? --Same technique as the last question. We can use
Runner pointerTo detect if there is a loop. Remember that when I ran 1500 meters in school... Similarly, if a linked list has a ring and enters the ring, it will keep going in the ring, and the slow one will always be quickly caught up with the ring. So we use two pointers, one slow runner, and one fast runner. Each time slow goes one, and fast goes two. If slow = fast at a certain time point, it turns out that there is a ring.
How to determine which node is the start of the ring?-- This problem can be easily figured out.When the two pointers met for the first time, they had k elements before the Start Node of the ring, and these k elements were the number of linked list elements before they entered the ring.How can we prove it? Imagine that the two runner nodes start from the same point of a ring at the same time, and the fast runner advances at a speed of 2 times slower than the Slow Runner. Their first coincidence must be at this starting point, each meeting in the future must be at this starting point. So, imagine that the K nodes before entering the ring are part of the ring, and the distance from the real Start Node of the ring is K, then the two runner will start at the same time, the first collision must be at the Start Node K of the ring.Therefore, during the first collision, any runner is randomly returned to the head of the linked list. At this time, two runner nodes are moving forward at the same speed. When they encounter another node, It is the starting node of the ring.
Java: http://pastebin.com/p35Cwgzv
public static LinkedListNode findBegin(LinkedList list){2. LinkedListNode slow = list.head;3. LinkedListNode fast = list.head;4. 5. /*find the first meeting point*/6. while(fast!=null){7. fast = fast.next.next;8. slow = slow.next;9. if(fast == slow){10. break;11. }12. }13. 14. /*check if there do have a loop*/15. if(fast == null){16. return null;17. }18. 19. slow = list.head;20. 21. /*find the beginning of the loop*/22. while(slow!=fast){23. slow = slow.next;24. fast = fast.next;25. }26. return fast;27. }
4. Implement a function to check if a linked list is a palindrome. Check whether the linked list is a text-back linked list.
Source: cracking the code interview (2nd edition) CH2 q7.
[Answer]: It's a classic interview question. Enter 0 --> 1 --> 2 --> 1 --> 0 odd nodes, or 0 --> 1 --> 2 --> 2 --> 1 --> 0 even nodes.
Method 1:The natural idea is to reverse the linked list, and then compare the node values of the linked list and the original linked list after the reverse, as long as the elements of the reverse linked list and the original linked list can be compared.
Method 2:Use runner pointer. This method can be used to solve many problems.Runner pointer and Recursion are the two most common methods to solve the linked list problem.
Define two pointers: Slow Runner and fast runner. Fast moves to the end of the linked list at twice the speed of slow. If there are an odd number of nodes: When fast reaches the end of the linked list, slow just exists in the center of the linked list. If there are even nodes: When fast is null for the first time, slow completes access to half of the nodes. Press the element values of nodes accessed by slow into a stack. After that, slow continues to access the end of the linked list. At the same time, the elements in the stack start to exit the stack (when there are odd elements, the intermediate elements must be skipped), and the values of the two elements are compared, it is not a reply. Otherwise, it is. The time complexity of this solution is O (n), because an iteration is required and the space of O (n) is used as a stack to store linked list elements.
Java code http://pastebin.com/wZ54iSmq here the list is the data structure defined at the beginning of this article.
1. public static boolean isPalindrome(LinkedList list){2. LinkedListNode slow = list.head;3. LinkedListNode fast = list.head;4. 5. Stack<Integer> stack = new Stack<Integer>();6. while(fast!=null&&fast.next!=null){7. stack.push(slow.value);8. slow = slow.next;9. fast = fast.next.next;10. }11. 12. /*if the length of list is odd*/13. if(fast!=null){14. slow = slow.next;15. }16. 17. /*compare elements all the way to the end*/18. while(slow!=null){19. if(slow.value!=stack.pop().intValue()){20. return false;21. }22. slow = slow.next;23. }24. return true;25. }
Reprinted: http://blog.csdn.net/tanglinghui/article/details/7618142