{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Linked Lists - delete specific target initially pointer =...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Linked lists Linked List 3 4 ^ 7 insert 5 3 4 5 7 Each entry in a linked list is a “node” object ^ front class Node< T> { T data ; Node< T> next ; public Node( T data, Node< T> next) { this . data = data; this . next = next; } } data next 3 \ front Building a Linked List Node<Integer> front = null ; // add 3 to front of list Node<Integer> item = new Node<Integer>(3, null ); front = item; // add 4 to front of list item = new Node<Integer>(4, front); front = item; // address of item is copied into front polynomial homework multiplying = adding multiple times zero polynomial = front is null stack implementation using linked list push (a) - create a new node and add it to the front (front is top); O(1) pop (a) - deletes the front; O(1) queue implementation using linked list -- use a pointer “rear” at the last entry enqueue(a) - add new node to the end; O(1) dequeue(a) - delete the front; O(1)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
sequential search for linked list using a pointer and until the pointer = null, compare the pointer.data with the target and then do pointer=pointer.next if there is no match
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: delete specific target initially pointer = front lag = null after check fails lag = pointer pointer = point.next after node is deleted, lag .next = pointer.next Reversing a linked list recursive view of linked list bad words for recursion while until repeatedly every time implementation public static <T> Node<T> reverse(Node<T> front) { if (front == null || front.next == null) { if first is true, second part of the condition is not evaluated return front; } Node<T> rev = reverse(front.next); front.next.next = front; front.next = null; return rev; } O(n) time merging two sorted linked lists into a single sorted linked list (no new nodes) list 1 list 2 result public static <T> Node<T> merge(Node<T> L1, Node<T> L2) { if (L1 == null) { return L2;} if (L2 == null) { return L1;} if (L1.data.compareTo(L2.data) < 0) { L1.next = merge(L1.next, L2); return L1; L2.next = merge(L1, L2.next); return L2;...
View Full Document

{[ snackBarMessage ]}

Page1 / 3

Linked Lists - delete specific target initially pointer =...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online