1 / 18

Linked Lists

Linked Lists. Merge Sorted Lists. Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list The two input lists can be destroyed The output list should not contain any duplicate values Unnecessary nodes should be freed.

winfreds
Download Presentation

Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists

  2. Merge Sorted Lists • Write an algorithm that merges two sorted linked lists • The function should return a pointer to a single combined list • The two input lists can be destroyed • The output list should not contain any duplicate values • Unnecessary nodes should be freed

  3. Merge Sorted Lists –Iterative Solution Concept • Traverse lists L1 and L2 using two pointers, and compare elements • If the element from L1 is smaller – move to the next one • If the element from L2 is smaller – insert it into L1 (before the compared L1 element)

  4. Merge Sorted Lists –Iterative Solution MERGE-LISTS (L1, L2) // Simple case – point head to first node of L1. if (L1.val <= L2.val) headL1 else // Special case - first node in L2 is smaller. headL2 // Smallest value is first. temp L2.next // Save pointer to next. L2.nextL1 // The rest of L1 follows. L2temp // Point to next node in L2. prevhead // Save for future insertions.

  5. Merge Sorted Lists –Iterative Solution (continued) // Traverse both lists, insert L2 nodes into L1. while (L1 != null && L2 != null) if (L1.val < L2.val) if (L1.next = null) L1.next L2 // Append remainder of L2. L1null // Exit loop. else prevL1 // Save pointer to previous. L1L1.next // Move to next node.

  6. Merge Sorted Lists –Iterative Solution (continued) else if (L1.val > L2.val) temp L2.next // Save pointer to next. L2.nextL1 // Insert L2 node before L1. prev.nextL2 L2temp // Point to next node in L2. else // (L1.val = L2.val) tempL2 // Save pointer for freeing. L2L2.next // Duplicate – skip it. free (temp) returnhead

  7. Merge Sorted Lists –Recursive Solution MERGE-LISTS (L1, L2) switch caseL1 = null: headL2 caseL2 = null:headL1 caseL1.val < L2.val: headL1 head.next  MERGE-LISTS (L1.next, L2) caseL1.val > L2.val: headL2 head.next  MERGE-LISTS (L1, L2.next) caseL1.val = L2.val: head  L1 head.next  MERGE-LISTS (L1.next, L2.next) free (L2) returnhead

  8. Merge Unsorted Lists • What if the lists are not sorted? • If we also want to remove duplicates, must compare each element in one list, with all elements in the other • Complexity: O(n2) • If we give up on this requirement,can we do better?

  9. Merge Unsorted Lists • Yes: simply append the lists APPEND-LIST (L1, L2) if (L1 = null) L1  L2 else curL1 while (cur.next != null) curcur.next cur.next L2

  10. Reverse Linked List • Write a function that accepts a linked list as input, and reverses it in place • No nodes should be freed or created –only the pointers should be changed • We will start by writing an iterative solution, and then give a recursive one

  11. Iterative Reverse ITERATIVE_REVERSE (list) resultnull curlist while (cur != null) nextcur.next cur.next result resultcur curnext listresult

  12. Iterative Reverse –Java Implementation publicvoid iterativeReverse() { ListNode result = null, cur = head, next; while (cur != null) { next = cur.getNext(); cur.setNext (result); result = cur; cur = next; } head = result; }

  13. Recursive Reverse • The straightforward solution: • Save the first element • Reverse the rest of the list • Append the first element and make it last • This will work, but instead of O(n) for the iterative solution, will take O(n2) • Is there an O(n) recursive solution?

  14. Recursive Reverse – O(n2) RECURSIVE_REVERSE (list) if (list = null) return null firstlist restlist.next if (rest = null) returnlist rest RECURSIVE_REVERSE (rest) cur rest while (cur.next != null) curcur.next cur.next first first.next null returnrest

  15. Recursive Reverse – O(n) RECURSIVE_REVERSE (list) if (list = null) return null firstlist restlist.next if (rest = null) returnlist rest RECURSIVE_REVERSE (rest) first.next.next first first.next null returnrest

  16. Recursive Reverse –Java Implementation • When writing pseudocode, a list is simply a pointer to the first element • The type of the list is "pointer to list node" • In Java, the list is wrapped by a class, which contains a head pointer as a field • The list has a type of its own • Only the head field itself is of type "pointer to list node"

  17. Recursive Reverse –Java Implementation • The recursive call needs to get a pointer to the list head as a parameter, but this pointer is a private field • Therefore, a wrapper method should be provided to expose the functionality: public void reverse() { head = reverseRec (head); }

  18. Recursive Reverse –Java Implementation private static ListNode reverseRec (ListNode list) { ListNode first, rest; if (list == null) return null; first = list; rest = list.getNext(); if (rest == null) return list; rest = reverseRec (rest); first.getNext().setNext (first); first.setNext (null); return rest; }

More Related