Statement:The linked list is defined as follows:
//Java:class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}
//C++:typedef struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }}ListNode;
?
Delete a node from a single-chain table with no Headers
Details:For a single-chain table without a header pointer, if a pointer points to a node in the middle of the single-chain table (not the first or the last node), delete the node from the single-chain table.
Question:
Solution 1: because a single-chain table does not give a pointer to the node, we cannot find the previous node of the node through the traversal table to change its next point to the next node of the node. In another way, we can replace all the element values of the node with its next node, and then delete the next node, which is equivalent to deleting the node.
//Javapublic void deleteRandomNode(ListNode currentNode) { ListNode nextNode = currentNode.next; if (nextNode != null) { currentNode.val = nextNode.val; currentNode.next = nextNode.next; } nextNode = null;}
//C++void deleteRandomNode(ListNode *current){ ListNode *next = current->next; if (next != NULL){ current->val = next->val; current->next = next->next; } delete next;}
?
Reverse linked list
Details:Given the head pointer of a linked list, it is required to traverse only once and reverse the element order in the single-chain table.
Question:
Solution 1: The question is relatively simple. Record the pointer of the next node each time it is reversed.
//Javapublic ListNode ReverseList(ListNode head) { ListNode pre = null, next = null; while (head != null) { next = head.next; head.next = pre; pre = head; head = next; } return pre;}
//C++ListNode *ReverseList(ListNode *pHead) { ListNode *current = NULL, *prev = NULL; while (pHead != NULL) { current = pHead; pHead = pHead->next; current->next = prev; prev = current; } return current;}
?
The first public node of the two linked lists
Details:Enter two linked lists to find their first public node.
Question:
Solution 1: In order to find the public nodes of two linked lists, We can traverse from the end to the head, but only give us the first node, so similar to the stack's advanced and later, therefore, we can use two stacks to save the nodes and then retrieve the nodes from the stack for comparison.
Solution 2: Calculate the length of the two linked lists (len1 and len2), let the longer linked list go first-ABS (len1-len2) 'length, and then the two will continue to traverse at the same time, find the first public node.
//C++ListNode *FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2) { int len1 = SizeLinkedList(pHead1); int len2 = SizeLinkedList(pHead2); if (len1 > len2) { pHead1 = walker(pHead1, len1 - len2); } else { pHead2 = walker(pHead2, len2 - len1); } while (pHead1->val != pHead2->val) { pHead1 = pHead1->next; pHead2 = pHead2->next; } return pHead1;}int SizeLinkedList(ListNode *head) { if (head == NULL) return 0; int size = 0; ListNode *current = head; while (current != NULL) { size++; current = current->next; } return size;}ListNode *walker(ListNode *head, int cnt) { while (cnt--) { head = head->next; } return head;}
?
Determine whether a given linked list has a ring
Details:Given a linked list to determine whether the linked list has a ring
Question:
Solution 1: Floyd ring Determination Algorithm
//C++bool hasRing(ListNode *pHead){ bool hasRing = false; ListNode *fast = pHead, *slow = pHead; while (fast != NULL && fast->next != NULL){ fast = fast->next->next; slow = slow->next; if (fast == slow) hasRing = true; } return hasRing;}
?
Central entry node of the linked list
Details:For a linked list, if it contains a ring, find the entry point of the ring of the linked list. Otherwise, the output is null.
Question:
Solution 1: Floyd ring Determination Algorithm
//Javapublic ListNode EntryNodeOfLoop(ListNode pHead) { ListNode fast = pHead, slow = pHead; while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; if (fast == slow) break; } if (fast == null || fast.next == null) return null; fast = pHead; while (fast != slow) { fast = fast.next; slow = slow.next; } return fast;}
//C++ListNode *EntryNodeOfLoop(ListNode *pHead) { ListNode *slow = pHead, *fast = pHead; while (fast != NULL && fast->next != NULL) { fast = fast->next->next; slow = slow->next; if (fast == slow) break; } if (fast == NULL || fast->next == NULL) return NULL; fast = pHead; while (fast != slow) { fast = fast->next; slow = slow->next; } return fast;}
?
Determine whether two linked lists are intersecting
Details:The first pointer of two single-chain tables is given to determine whether the two linked lists are intersecting.
Question:
Solution 1: If two linked lists overlap, the last node of the linked list must be public, so we can use this property to solve the problem.
//C++bool isIntersect(ListNode *pHead1, ListNode *pHead2){ if (pHead1 == NULL || pHead2 == NULL) return false; while (pHead1->next != NULL) pHead1 = pHead1->next; while (pHead2->next != NULL) pHead2 = pHead2->next; if (pHead1 == pHead2) return true; return false;}
Solution 2: because they are all single linked lists, that is, there is no ring, we can link the first linked list to the back of the second linked list. If the new linked list has a ring, it proves that there are public nodes.
//C++bool isIntersect(ListNode *pHead1, ListNode *pHead2){ if (pHead1 == NULL || pHead2 == NULL) return false; pHead1->next = pHead2; return hasRing(pHead1);}
?
Determine whether two linked lists are intersecting
Deformation
Details:Given twoRingThe head pointer of the linked list to determine whether the two linked lists are intersecting.
Question:
Solution 1: For a chain table, if the table has an intersection, the following situations exist:
Therefore, find the entry node of the linked list and determine whether it is equal. The corresponding scenario is 1 and 2. For 3, we can fix a node, and then determine whether there is an intersection in the traversal chain table.
//C++bool isIntersect(ListNode *pHead1, ListNode *pHead2){ if (pHead1 == NULL || pHead2 == NULL) return false; ListNode *entry1 = EntryNodeOfLoop(pHead1); ListNode *entry2 = EntryNodeOfLoop(pHead2); if (entry1 == entry2) return true; else{ ListNode *backup = entry2; do { entry2 = entry2->next; }while (entry2 != entry1 && entry2 != backup); return entry2 != backup; }}
?
Merge two sorted linked lists
Details:Input two monotonically incrementing linked lists and output the merged linked lists. Of course, the merged linked lists meet the monotonous rules.
Question:
Solution 1:
//Javapublic ListNode Merge(ListNode list1, ListNode list2) { if (list1 == null) { return list2; } if (list2 == null) { return list1; } ListNode prev = null; ListNode root = list1.val < list2.val ? list1 : list2; while (list1 != null && list2 != null) { if (list1.val < list2.val) { if (prev == null) { prev = list1; } else { prev.next = list1; prev = list1; } list1 = list1.next; } else { if (prev == null) { prev = list2; } else { prev.next = list2; prev = list2; } list2 = list2.next; } } while (list1 != null) { prev.next = list1; prev = list1; list1 = list1.next; } while (list2 != null) { prev.next = list2; prev = list2; list2 = list2.next; } return root;}
//C++ListNode *Merge(ListNode *pHead1, ListNode *pHead2) { if (pHead1 == NULL) return pHead2; if (pHead2 == NULL) return pHead1; ListNode *prev = NULL; ListNode *root = pHead1->val < pHead2->val ? pHead1 : pHead2; while (pHead1 != NULL && pHead2 != NULL) { if (pHead1->val < pHead2->val) { if (prev == NULL) { prev = pHead1; } else { prev->next = pHead1; prev = pHead1; } pHead1 = pHead1->next; } else { if (prev == NULL) { prev = pHead2; } else { prev->next = pHead2; prev = pHead2; } pHead2 = pHead2->next; } } while (pHead1 != NULL) { prev->next = pHead1; prev = pHead1; pHead1 = pHead1->next; } while (pHead2 != NULL) { prev->next = pHead2; prev = pHead2; pHead2 = pHead2->next; } return root;}
Linked List topic-linked list problems frequently encountered during interviews