1. Given a pointer/reference to the head node of a linked list, write a method t
ID: 3589421 • Letter: 1
Question
1. Given a pointer/reference to the head node of a linked list, write a method to display its elements, one element per line. If the head pointer is null (indicating the list is empty), don’t display anything.(java code)
2. Reverse a singly linked list. Write reverseIteratively() method which reverses a single linked list using a three-pointers approach and using loops. It traverses through the linked list and adds nodes at the beginning of the singly linked list in each iteration. It uses three reference variables (pointers) to keep track of previous, current and next nodes.(java code)
3. Find Union and Intersection of two sets. Given two sets (elements in a set are distinct), write a method that finds the union and intersection of two sets using linked list. E.g., given A= {6,1,3, 20,7 }, B={2,6,8,3,5} your algorithm should print Union as {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6}. Note that the elements of union and intersection can be printed in any order. (java code)
4. Write a method rotate(list,k) which rotates an integer list by a given number k. k elements from the end should move to the beginning of the list, and all other elements should move to right to make the space. E.g., if list a is initialized with elements l = {1, 2, 3, 4, 5, 6, 7, 8, 9}. (java code)
Divide the list into two parts Head First Rest Reverse Rest Head First Now wy need to remove this link a ceate a ink from node 2 to node 1 Rest Link rest to first first nextnext-first irst->next nu Head Change Head Rest 'head ref= rest 3 HeadExplanation / Answer
Hi, I have answered Q1.
Please repost other questions in separate post.
public class LinkedList
{
/* Linked list Node*/
static class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
/* Appends a new node at the end. This method is
defined inside LinkedList class shown above */
public static Node append(Node head, int new_data)
{
/* 1. Allocate the Node &
2. Put in the data
3. Set next as null */
Node new_node = new Node(new_data);
/* 4. If the Linked List is empty, then make the
new node as head */
if (head == null)
{
head = new Node(new_data);
return head;
}
/* 4. This new node is going to be the last node, so
make next of it as null */
new_node.next = null;
/* 5. Else traverse till the last node */
Node last = head;
while (last.next != null)
last = last.next;
/* 6. Change the next of last node */
last.next = new_node;
return head;
}
/* This function prints contents of linked list starting from
the given node */
public static void displayLinkedList(Node head)
{
if(head == null) {
System.out.println("List is empty");
return;
}
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
}
/* Driver program to test above functions. Ideally this function
should be in a separate user class. It is kept here to keep
code compact */
public static void main(String[] args)
{
/* Start with the empty list */
Node head = null;
// Insert 6. So linked list becomes 6->NUllist
head = append(head, 6);
// Insert 7 at the beginning. So linked list becomes
// 7->6->NUllist
head = append(head, 7);
// Insert 1 at the beginning. So linked list becomes
// 1->7->6->NUllist
head = append(head, 1);
// Insert 4 at the end. So linked list becomes
// 1->7->6->4->NUllist
head = append(head, 4);
// Insert 8, after 7. So linked list becomes
// 1->7->8->6->4->NUllist
head = append(head, 8);
System.out.println("Linked list is: ");
displayLinkedList(head);
}
}
/*
Sample run:
Linked list is:
6 7 1 4 8
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.