Download presentation

Presentation is loading. Please wait.

Published byGaige Asch Modified over 3 years ago

1
Linked Lists

2
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
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
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
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
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) return head

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

8
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(n 2 ) If we give up on this requirement, can we do better?

9
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
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
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
12 Iterative Reverse – Java Implementation public void iterativeReverse() { ListNode result = null, cur = head, next; while (cur != null) { next = cur.getNext(); cur.setNext (result); result = cur; cur = next; } head = result; }

13
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(n 2 ) Is there an O(n) recursive solution?

14
14 Recursive Reverse – O(n 2 ) RECURSIVE_REVERSE (list) if (list = null) return null first list rest list.next if (rest = null) return list rest RECURSIVE_REVERSE (rest) cur rest while (cur.next != null) cur cur.next cur.next first first.next null return rest

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

16
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
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
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; }

Similar presentations

OK

CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.

CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on ready to serve beverages and more locations Ppt on quality education fund Ppt on product advertising letter Ppt on aerobics steps Ppt on library management system project in java Jit ppt on manufacturing Ppt on causes of 1857 revolt leaders Ppt on supply chain management in india Open ppt on ipad keynote Ppt on revolt of 1857 ppt