0% found this document useful (0 votes)
11 views

Linked List

linked list prep for interview

Uploaded by

Dhruv Sompura
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Linked List

linked list prep for interview

Uploaded by

Dhruv Sompura
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

linked list

insert new node

public static Node insertAfter(Node head, int key,


int newData)
{
Node curr = head;

while (curr != null) {


if (curr.data == key)
break;
curr = curr.next;
}

if (curr == null)
return head;

Node newNode = new Node(newData);


newNode.next = curr.next;
curr.next = newNode;
return head;
}

---------------------------------------------------------------------------------

palindrome check

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) {
return true;
}

ListNode slow = head;


ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}

ListNode reversedSecondHalf = reverse(slow);

while (reversedSecondHalf != null) {


if (head.val != reversedSecondHalf.val) {
return false;
}
head = head.next;
reversedSecondHalf = reversedSecondHalf.next;
}

return true;
}

private ListNode reverse(ListNode head) {


ListNode prev = null;
ListNode current = head;
ListNode next = null;

while (current != null) {


next = current.next;
current.next = prev;
prev = current;
current = next;
}

return prev;
}
}

-------------------------------------------------------------------------

reverse linked list

Node reverse(Node node)


{
Node prev = null;
Node curr = head;
Node temp = null;
while (curr != null) {
temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
return prev;
}

tcncnppcct
-------------------------------------------------------------

recurssively:

void reverse(Node p)
{
if(p.next==null)
{
head=p;
return;
}
reverse(p.next);
Node q=p.next;
q.next=p;
p.next=null;
}
qpnqnppn0

//pehle recursively list ke end tak pocho


//fir jab recursion me piche piche hoga vaise vaise values naye node me daalte jao
https://www.youtube.com/watch?v=KYH83T4q6Vs&ab_channel=mycodeschool
-------------------------------------------------------------
if cycle present in linked list or not

/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow=head;
ListNode fast=head;
while(fast != null && fast.next != null)
{
slow=slow.next;
fast=fast.next.next;
if(slow==fast)
{
return true;
}
}
return false;
}
}

----------------------------------------------------------------

middle of the linked list

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode middleNode(ListNode head) {
ListNode pt=head;
while(pt!=null && pt.next!=null)
{
head=head.next;
pt=pt.next.next;
}
return head;
}
}
-----------------------------------------------------------------------

delete a whole linked list

ListNode X = head;
head = head.next;
X = null;
-----------------------------------------------------------------

delete a node, given position

for (int i = 2; i < position; i++) {


if (temp.next != null) {
temp = temp.next;
}
}
temp.next = temp.next.next;
------------------------------------------------------------------

forward and backward traversal in doubly linked list

static void forwardTraversal(Node head) {


Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}

static void backwardTraversal(Node tail) {


Node curr = tail;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.prev;
}

// Print newline after traversal


System.out.println();
}

-----------------------------------------------------------------
insert node on double linked list

public static Node insertAtPosition(Node head, int pos, int new_data) {

Node new_node = new Node(new_data);

if (pos == 1) {
new_node.next = head;
if (head != null) {
head.prev = new_node;
}
head = new_node;
return head;
}
Node curr = head;

for (int i = 1; i < pos - 1 && curr != null; ++i) {


curr = curr.next;
}

// If the position is out of bounds


if (curr == null) {
System.out.println("Position is out of bounds.");
return head;
}

new_node.prev = curr;
new_node.next = curr.next;
curr.next = new_node;

return head;
}
------------------------------------------------------------------

delete node in double linked list

public static Node delPos(Node head, int pos) {

if (head == null) {
return head;
}

Node curr = head;

for (int i = 1; curr != null && i < pos; ++i) {


curr = curr.next;
}

// If the position is out of range


if (curr == null) {
return head;
}

// Update the previous node's next pointer


if (curr.prev != null) {
curr.prev.next = curr.next;
}

// Update the next node's prev pointer


if (curr.next != null) {
curr.next.prev = curr.prev;
}

// If the node to be deleted is the head node


if (head == curr) {
head = curr.next;
}

// Return the updated head


return head;
}
--------------------------------------------------------------------------
insert circular list

static Node insertAtPosition(Node last, int data,


int pos){
if (last == null) {
// If the list is empty
if (pos != 1) {
System.out.println("Invalid position!");
return last;
}
// Create a new node and make it point to itself
Node newNode = new Node(data);
last = newNode;
last.next = last;
return last;
}

// Create a new node with the given data


Node newNode = new Node(data);

// curr will point to head initially


Node curr = last.next;

if (pos == 1) {
// Insert at the beginning
newNode.next = curr;
last.next = newNode;
return last;
}

// Traverse the list to find the insertion point


for (int i = 1; i < pos - 1; ++i) {
curr = curr.next;

// If position is out of bounds


if (curr == last.next) {
System.out.println("Invalid position!");
return last;
}
}

// Insert the new node at the desired position


newNode.next = curr.next;
curr.next = newNode;

// Update last if the new node is inserted at the


// end
if (curr == last)
last = newNode;

return last;
}

---------------------------------------------------------------------------

delete circular list

public static Node deleteSpecificNode(Node last,


int key){
if (last == null) {
// If the list is empty
System.out.println(
"List is empty, nothing to delete.");
return null;
}
Node curr = last.next;
Node prev = last;

// If the node to be deleted is the only node in the


// list
if (curr == last && curr.data == key) {
last = null;
return last;
}

// If the node to be deleted is the first node


if (curr.data == key) {
last.next = curr.next;
return last;
}

// Traverse the list to find the node to be deleted


while (curr != last && curr.data != key) {
prev = curr;
curr = curr.next;
}

// If the node to be deleted is found


if (curr.data == key) {
prev.next = curr.next;
if (curr == last) {
last = prev;
}
}
else {
// If the node to be deleted is not found
System.out.println("Node with data " + key
+ " not found.");
}
return last;
}
------------------------------------------------------------

You might also like