









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










class Node { int data; Node next; public Node() { data = 0 ; next = null ; } public Node( int data) { this .data = data; this .next = null ; } }
Node p = new Node( 10 );
p.next = new Node(15);
p.next.next = new Node(30);
import java.util.*; class Node {
class LinkedList { Node head, tail; public LinkedList() { head = null ; tail = null ; } public boolean Empty() { return head == null ; } }
LinkedList list = new LinkedList();
Node temp = new Node( 5 );
temp.next = head;
head = temp;
public void addFirst( int val) { if (tail == null ) // list is empty head = tail = new Node(val); else { Node temp = new Node(val); temp.next = head; head = temp; } }
tail.next = new Node( 40 );
tail = tail.next;
return true ; }
public boolean removeLast() { if (Empty()) return false ; if (head == tail) // only one node in a list { head = tail = null ; } else // if more than one node in the list { Node temp = head; // find the predecessor of tail while (temp.next != tail) temp = temp.next; tail = temp; // the predecessor of tail becomes tail tail.next = null ; } return true ; }
temp = head;
while (temp.next != tail) temp = temp.next;
tail = temp; tail.next = null ;
import java.util.*; class ListNode { int data; ListNode next; public ListNode( int data) { this .data = data; this .next = null ; } } class LinkedList { ListNode head, tail; public LinkedList() { head = null ; tail = null ; } public boolean Empty() { return head == null ; } public void addFirst( int val) { if (tail == null ) // list is empty head = tail = new ListNode(val); else { ListNode temp = new ListNode(val); temp.next = head; head = temp; } } public void addLast( int val)
list.addFirst(77); list.addFirst(99); // 99 77 20 15 30 35 list.Print(); list.removeFirst(); list.removeFirst(); list.removeFirst(); list.removeLast(); // 15 30 list.Print(); } }
#include <stdio.h> class ListNode { public: int data; ListNode *next; ListNode(int data) : data(data), next(NULL) {} }; class LinkedList { public: ListNode *head, *tail; LinkedList() { head = NULL; tail = NULL; } bool Empty() { return head == NULL; } void addFirst(int val) { if (tail == NULL) // list is empty head = tail = new ListNode(val); else { ListNode *temp = new ListNode(val); temp->next = head; head = temp; } } void addLast(int val) { if (tail != NULL) // list is not empty { tail->next = new ListNode(val); tail = tail->next; } else head = tail = new ListNode(val); }
bool removeFirst() { if (Empty()) return false; if (head == tail) // only one node in a list head = tail = NULL; else head = head->next; return true; } bool removeLast() { if (Empty()) return false; if (head == tail) // only one node in a list { head = tail = NULL; } else // if more than one node in the list { ListNode *temp = head; // find the predecessor of tail while (temp->next != tail) temp = temp->next; tail = temp; // the predecessor of tail becomes tail tail->next = NULL; } return true; } void Print() { ListNode *head = this->head; while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } }; ListNode *temp; int main(void) { LinkedList *list = new LinkedList(); if (list->Empty()) printf("Empty\n"); list->addFirst(10); list->addFirst(15); list->removeLast(); list->addFirst(20); list->Print(); // 20 15 list->addLast(30); list->addLast(35); list->addFirst(77); list->addFirst(99); list->Print(); // 99 77 20 15 30 35 list->removeFirst(); list->removeFirst(); list->removeFirst(); list->removeLast(); list->Print(); // 15 30 return 0; }
p, q
p q
p q
p, q
// C++ implementation int hasCycle(ListNode head) { if (head == NULL) return 0; ListNode p = head; ListNode* q = head; while (q->next && q->next->next) { p = p->next; q = q->next->next; if (p == q) return 1; } return 0; } // Java implementation int hasCycle(ListNode head) { if (head == null ) return 0; ListNode p = head; ListNode q = head; while (q.next != null && q.next.next != null ) { p = p.next; q = q.next.next; if (p == q) return 1; } return 0; }
l 1 l 2 res 1 4 7 8 9 2 6 8 l 2 res l 1 1 4 7 8 9 2 6 8 l 2 res l 1 1 4 7 8 9 2 6 8 l 2 res l 1 1 4 7 8 9 2 6 8 l 2 res l 1 1 4 7 8 9 2 6 8 l 2 res l 1