|
|
Note:
|
|
ptr = head; while ( ptr.next != null ) // Last node has next == null { ptr = ptr.next; } |
Result:
![]() |
However:
|
|
1. Initialization: Make ptr1 points to the first Node Make ptr2 points to the second Node 2. While ( ptr2 ≠ last node ) do: { 1. move ptr1 forward 2. move ptr2 forward } When the while loop ends, we will have: ptr2 points to the last node ptr1 points to the one-before-the-last node |
|
Statement(s) to achieve this result:
ptr1.next = null; |
// Find the last *2* Nodes in the list /* ------------------------------------------------ Initial state: head ---> Node1 ---> Node2 ---> Node3 --->... ^ ^ | | ptr1 ptr ------------------------------------------------- */ Node ptr, ptr1; // ptr1 always points to the Node // before the one pointed to by ptr ptr1 = head; // ptr1 points to first node ptr = head.next; // ptr points to node following ptr1 while ( ptr.next != null ) { ptr1 = ptr; // Move to next pair of node ptr = ptr.next; } // ****************************************************** // ptr NOW points to the last Node in list // ptr1 NOW points to the Node BEFORE the last Node // ****************************************************** r = ptr.value; // Save the the value in last Node // (in case you want to return it) ptr1.next = null; // Mark the one-before-last node as the new last node |
|
if ( head.next == null ) Reason: head.next is the link in the first node If this link is null, then the first node is also the last node. The list has exactly 1 node.... |
An empty list is indicated using:
head = null; |
public class List { Node head; // Head is used to access the nodes // in the linked list /* ================================================ Constructor: initialize head to represent an empty list ================================================= */ public List() { head = null; // null means: no valid nodes } /* ============================================= The remove(x) method Note: I made the method return the value stored in the Node object ============================================= */ public double remove( ) throws Exception { /* ============================================== Can't delete from an empty list: error ============================================== */ if ( head == null ) throw new Exception("Remove from empty list"); /* ================================================= Here we delete the node at the tail of the list ================================================= */ double r; if ( head.next == null ) // List has 1 Node { r = head.value; // Save value to return it... head = null; // Mark list as "empty" } else { // Find the last *2* Nodes in the list /* ------------------------------------------------ Initial state: head ---> Node1 ---> Node2 ---> Node3 --->... ^ ^ | | ptr1 ptr ------------------------------------------------- */ Node ptr, ptr1; // ptr1 always points to the Node // before the one pointed to by ptr ptr1 = head; // ptr1 points to first node ptr = head.next; // ptr points to node following ptr1 while ( ptr.next != null ) { ptr1 = ptr; // Move to next pair of node ptr = ptr.next; } // ****************************************************** // ptr NOW points to the last Node in list // ptr1 NOW points to the Node BEFORE the last Node // ****************************************************** r = ptr.value; // Save the the value in last Node // (in case you want to return it) ptr1.next = null; // Mark the // one-before-last node as // the new last node } } } |
public static void main( String[] args ) { List myList1 = new List(); // Create 2 empty lists List myList2 = new List(); double x; x = myList1.remove(); // Delete in first list x = myList2.remove(); // Delete in second list } |
How to run the program:
|