00:00

Understanding Linked Lists and Recursion in Java

Linked lists are defined recursively in Java, with nodes either being empty or containing a reference to the next node. Learn how to read and modify linked lists recursively by implementing methods such as readThing, changeList, removeAll, duplicateEvens, and more. This comprehensive guide provides a step-by-step explanation of how to work with linked lists using recursion, covering base cases, modification of nodes, recursive calls, and proper linking of nodes. Master the concept of modifying linked data structures effectively by following the patterns presented in this Java guide.

pedrianes
Download Presentation

Understanding Linked Lists and Recursion in Java

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 and Recursion

  2. Linked Lists are defined recursively! • A linked is is either: • Empty (null reference) front • A ListNode with a reference to a linked list

  3. Reading a Linked List (Recursively) • Public-private pair: • Public method: • Call private method with argument front public int readThing(){ return readThing(front); } private int readThing(ListNode curr){ if (curr == null){ return 0; } else{ return 1 + readThing(curr.next); } } • Private method (recursive): • If the current node is null, you’ve reached the end! • Just return (base case) • If the current node is not null, there’s more list! • “Read” the current node • Keep going! Recursive call with argument curr.next

  4. Modifying a Linked List (Recursively) public void changeList(){ front = changeList(front); } private ListNode changeList(ListNode curr){ // The previous node will link to what we return if (curr == null){ // End/Last case. // Do we need to add a node here? return new ListNode(0); //if so, return it! } else{ // Middle Case // Our jobs: // 1) Modify the list at curr (e.g. add a node) // 2) Do a recursive call, get link to the node returned // 3) return what the previous node should link to curr.next = changeList(curr.next); return curr; } } • Public method: • Call private method with argument front • Assign return value to front • Private method (recursive): • If the current node is null, you’ve reached the end! • End/Last case! • If the current node is not null, there’s more list! • “modify” at the current node • Keep going! Recursive call with on curr.next • Assign return value to proper place

  5. x = change(x) • Pattern used to modify a linked data structure • E.g. linked lists and trees (soon!) • x is a reference to the first node in the data structure • change is a method that modifies a data structure, starting from the node x • It returns the “new” first thing

  6. Modifying a Linked List (Recursively) A chain of nodes, already modified, it will link to what we return A chain of nodes, not yet modified, we will link to what this returns curr What do we do with curr? 1) Check for base case 2) Modify the “neighborhood” of curr 3) Do a recursive call 4) Link to return value 5) Return node previous should link to

  7. A chain of nodes, already modified, it will link to what we return A chain of nodes, not yet modified, we will link to what this returns curr public void changeList(){ front = changeList(front); } private ListNode changeList(ListNode curr){ if (curr == null){ return new ListNode(0); } else{ curr.next = changeList(curr.next); return curr; } }

  8. removeAll() – data doesn’t match value A chain of nodes, already modified, it will link to what we return A chain of nodes, not yet modified, we will link to what this returns curr Curr.next = removeAll(value, curr.next); Return curr;

  9. removeAll() – data matches value A chain of nodes, already modified, it will link to what we return A chain of nodes, not yet modified, we will link to what this returns curr Return removeAll(value, curr.next)

  10. duplicateEvens() A chain of nodes, already modified, it will link to what we return A chain of nodes, not yet modified, we will link to what this returns 9 curr We want to return curr Curr.next = dulplicateEvens(curr.next)

  11. duplicateEvens() A chain of nodes, already modified, it will link to what we return A chain of nodes, not yet modified, we will link to what this returns 6 curr Return new ListNode(6, curr) Curr.next = duplicateEvens(curr.next)

More Related