Tags » Linked List

Remove Duplicates from Sorted List

Problem linkhttps://leetcode.com/problems/remove-duplicates-from-sorted-list/

Descriptions:

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2… 115 more words

Easy

Code #6 Hackerrank Data structure – Linked List #4

Fourth problem in data structure. Insert a node at a specific position in a linked list.

Node* InsertNth(Node *head, int data, int position)
{
Node *temp= new Node;
Node *ite= new Node;
temp->data = data;
if(position==0)
{
if(head==NULL)
temp->next=NULL;
else
temp->next=head;
head=temp;
}
else
{
ite=head;
while(position>1)
{
ite=ite->next;
position--;
}
temp->next=ite->next;
ite->next=temp;
}
return head;
}

Verdict:Easy

Data Structure

Code #4 Hackerrank Data structure - Linked List #2

In question 2 we have insert element at the end
my initial submission didn’t work as I traversed my head node till the end. Very Illogical of me. 53 more words

Data Structure

Code #3 Hackerrank Data structure - Linked List #1

Method based problem to print element of linked list.

void Print(Node *head)
{
if(head==NULL)
return;
while(head!=NULL)
{
std::cout<<head->data<<endl;
head=head->next;
}
return;
}

Verdict:  CAKEWALK!

Data Structure

Single Linked List

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int info;
node *next;
};
node *start,*p,*q,*end;
void main() {
int ch,n;
start=p=q=end=NULL;
do {
clrsc();
cout<<“1. Add new node”<<endl; 751 more words

Linked List

Linked list from scratch

In a linked list, each data item is embedded in a link. Each Link object contains a reference (usually called next) to the next link in the list. 349 more words

Sort List

Sort a linked list in O(n log n) time using constant space complexity.

    public ListNode sortList(ListNode head) {
        
        int len = length(head);
        
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        
        for (int size = 1; size < len; size += size) {
            ListNode newHead = new ListNode(0);
            ListNode newTail = newHead;
            while (dummy.next != null) {
                ListNode left = dummy.next;
                ListNode temp = dummy;
                for (int i = 0; i < size && temp.next != null; i++) {
                    temp = temp.next;
                }
                ListNode right = temp.next;
                temp.next = null;
                dummy.next = right;
                temp = dummy;
                for (int i = 0; i < size && temp.next != null; i++) {
                    temp = temp.next;
                }
                dummy.next = temp.next;
                temp.next = null;
                
                while (left != null || right != null) {
                    if (left == null) {
                        newTail.next = right;
                        newTail = newTail.next;
                        right = right.next;
                    } else if (right == null) {
                        newTail.next = left;
                        newTail = newTail.next;
                        left = left.next;
                    } else {
                        if (left.val < right.val) {
                            temp = left;
                            left = left.next;
                        } else {
                            temp = right;
                            right = right.next;
                        }
                        newTail.next = temp;
                        newTail = newTail.next;
                    }
                }
            }
            dummy = newHead;
        }
        return dummy.next;
    }
    
    private int length(ListNode head) {
        int i = 0;
        while (head != null) {
            i++;
            head = head.next;
        }
        return i;
    }
LeetCode