Skip to content

Commit 7891490

Browse files
committed
delete LL nodes
1 parent c5c4331 commit 7891490

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

DSA Crack Sheet/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
- [Clone a linked list with next and random pointer](https://practice.geeksforgeeks.org/problems/clone-a-linked-list-with-next-and-random-pointer/1# "view question") - [Cpp Solution](./solutions/Clone%20a%20linked%20list%20with%20next%20and%20random%20pointer.cpp)
156156
- [Merge K sorted linked lists](https://practice.geeksforgeeks.org/problems/merge-k-sorted-linked-lists/1# "view question") - [Cpp Solution](./solutions/Merge%20K%20sorted%20linked%20lists.cpp)
157157
- [Multiply two linked lists](https://practice.geeksforgeeks.org/problems/multiply-two-linked-lists/1# "view question") - [Cpp Solution](./solutions/Multiply%20two%20linked%20lists.cpp)
158+
- [Delete nodes having greater value on right](https://practice.geeksforgeeks.org/problems/delete-nodes-having-greater-value-on-right/1# "view question") - [Cpp Solution](./solutions/Delete%20nodes%20having%20greater%20value%20on%20right.cpp)
158159
- []( "view question") - [Cpp Solution](./solutions/.cpp)
159160

160161
### Binary Tree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Delete nodes having greater value on right
3+
==========================================
4+
5+
Given a singly linked list, remove all the nodes which have a greater value on its following nodes.
6+
7+
Example 1:
8+
Input:
9+
LinkedList = 12->15->10->11->5->6->2->3
10+
Output: 15 11 6 3
11+
Explanation: Since, 12, 10, 5 and 2 are
12+
the elements which have greater elements
13+
on their next node. So, after deleting
14+
them, the linked list would like be 15,
15+
11, 6, 3.
16+
17+
Example 2:
18+
Input:
19+
LinkedList = 10->20->30->40->50->60
20+
Output: 60
21+
Your Task:
22+
The task is to complete the function compute() which should modify the list as required and return the head of the modified linked list. The printing is done by the driver code,
23+
24+
Expected Time Complexity: O(N)
25+
Expected Auxiliary Space: O(1)
26+
27+
Constraints:
28+
1 <= size of linked list <= 1000
29+
1 <= element of linked list <= 1000
30+
Note: Try to solve the problem without using any extra space.
31+
*/
32+
33+
Node *compute(Node *head)
34+
{
35+
if (!head || !(head->next))
36+
return head;
37+
Node *temp = head, *prev = NULL, *nextnode;
38+
39+
while (temp && temp->next)
40+
{
41+
Node *temp2 = temp->next;
42+
while (temp2)
43+
{
44+
if (temp2->data > temp->data)
45+
break;
46+
temp2 = temp2->next;
47+
}
48+
49+
if (temp2)
50+
{
51+
nextnode = temp->next;
52+
if (prev == NULL)
53+
head = nextnode;
54+
else
55+
prev->next = nextnode;
56+
free(temp);
57+
temp = nextnode;
58+
}
59+
else
60+
{
61+
prev = temp;
62+
temp = temp->next;
63+
}
64+
}
65+
return head;
66+
}

0 commit comments

Comments
 (0)