-
Notifications
You must be signed in to change notification settings - Fork 210
/
Linkedlist.cpp
95 lines (85 loc) · 1.78 KB
/
Linkedlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node()
{
data = 0;
next = NULL;
}
};
/*Reverse a Linked List
Node *reverselist(Node *head)
{
Node *temp = NULL;
Node *temp2 = NULL;
while (head != NULL)
{
temp2 = head->next;
head->next = temp;
temp = head;
head = temp2;
}
return head;
}*/
/*Middle of linked list Tortoise and Hare approach
Node *middle(Node *head)
{
Node *slow = head;
Node *fast = head;
while (fast != NULL && fast != NULL)
{
slow = slow->next;
fast = fast->next->next;
}
return slow;
}*/
/*merge two sorted linked list
Node *merge(Node *head1, Node *head2)
{
if (head1 == NULL)
return head2;
if (head2 == NULL)
return head1;
if (head1->data >= head2->data)
std::swap(head1, head2);
Node *res = head1;
while (head1 != NULL && head2 != NULL)
{
Node *temp = NULL;
while (head1 != NULL && head1->data <= head2->data)
{
temp = head1;
head1 = head1->next;
}
temp->next = head2;
std::swap(head1, head2);
}
return res;
}*/
/*Remove nth node from back of the list
Node * remove(Node* head,int n)
{
Node *dummy = new Node();
dummy->next= head;
Node*fast = dummy;
Node*slow = dummy;
for(int i=1;i<=n;i++)
fast = fast->next;
while(fast->next!=NULL)
{
slow= slow->next;
fast =fast->next;
}
slow->next = slow->next->next;
return dummy->next;
}*/
/*Delete NTh node
Node *remove(Node *t)
{
t->data = t->next->data;
t->next = t->next->next;
}*/