-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from AbhiSaphire/pc-upload
Linked List
- Loading branch information
Showing
10 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
Coding Club India/Asked Amazon Interview Questions/Add2NumberLL.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
def reverse(ll): | ||
curr = ll | ||
forward = prev = None | ||
while curr != None: | ||
forward = curr.next | ||
curr.next = prev | ||
prev = curr | ||
curr = forward | ||
ll = prev | ||
return ll | ||
|
||
def addLists(first, second): | ||
first = reverse(first) | ||
second = reverse(second) | ||
prev = None | ||
temp = None | ||
carry = 0 | ||
res = LinkedList() | ||
while(first is not None or second is not None): | ||
fdata = 0 if first is None else first.data | ||
sdata = 0 if second is None else second.data | ||
Sum = carry + fdata + sdata | ||
carry = 1 if Sum >= 10 else 0 | ||
Sum = Sum if Sum < 10 else Sum % 10 | ||
temp = Node(Sum) | ||
|
||
if res.head is None: | ||
res.head = temp | ||
else : | ||
prev.next = temp | ||
|
||
prev = temp | ||
|
||
if first is not None: | ||
first = first.next | ||
if second is not None: | ||
second = second.next | ||
|
||
if carry > 0: | ||
temp.next = Node(carry) | ||
|
||
res.head = reverse(res.head) | ||
return res.head | ||
|
||
# Input - 4 5, 3 4 5 || Output - 3 9 0 | ||
# Explanation - | ||
# 4->5 | ||
# 3->4->5 | ||
# ------- | ||
# 3->9->0 |
23 changes: 23 additions & 0 deletions
23
Coding Club India/Asked Amazon Interview Questions/DeleteMidLL.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
''' | ||
class Node: | ||
def __init__(self, data): | ||
self.data = data | ||
self.next = None | ||
''' | ||
def deleteMid(head): | ||
''' | ||
head: head of given linkedList | ||
return: head of resultant llist | ||
''' | ||
if head == None or head.next == None: | ||
return None | ||
|
||
slow = fast = prev = head | ||
while slow and fast and fast.next: | ||
prev = slow | ||
slow = slow.next | ||
fast = fast.next.next | ||
|
||
prev.next = slow.next | ||
|
||
return head |
23 changes: 23 additions & 0 deletions
23
Coding Club India/Asked Goldman Sach Interview Questions/DeleteWithoutHeadPointer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#User function Template for python3 | ||
''' | ||
Your task is to delete the given node from | ||
the linked list, without using head pointer. | ||
Function Arguments: node (given node to be deleted) | ||
Return Type: None, just delete the given node from the linked list. | ||
{ | ||
# Node Class | ||
class Node: | ||
def __init__(self, data): # data -> value stored in node | ||
self.data = data | ||
self.next = None | ||
} | ||
Contributed By: Nagendra Jha | ||
''' | ||
def deleteNode(curr_node): | ||
temp = curr_node.next | ||
curr_node.data = temp.data | ||
curr_node.next = temp.next | ||
temp = None |
50 changes: 50 additions & 0 deletions
50
Coding Club India/Asked Goldman Sach Interview Questions/MergeSortLL.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
def split(tempHead): | ||
fast = slow = tempHead | ||
while(True): | ||
if fast.next is None: | ||
break | ||
if fast.next.next is None: | ||
break | ||
fast = fast.next.next | ||
slow = slow.next | ||
|
||
temp = slow.next | ||
slow.next = None | ||
return temp | ||
|
||
def merge(first, second): | ||
|
||
# If first linked list is empty | ||
if first is None: | ||
return second | ||
|
||
# If secon linked list is empty | ||
if second is None: | ||
return first | ||
|
||
# Pick the smaller value | ||
if first.data < second.data: | ||
first.next = merge(first.next, second) | ||
first.next.prev = first | ||
first.prev = None | ||
return first | ||
else: | ||
second.next = merge(first, second.next) | ||
second.next.prev = second | ||
second.prev = None | ||
return second | ||
|
||
def sortDoubly(tempHead): | ||
if tempHead is None: | ||
return tempHead | ||
if tempHead.next is None: | ||
return tempHead | ||
|
||
second = split(tempHead) | ||
|
||
# Recur for left and righ halves | ||
tempHead = sortDoubly(tempHead) | ||
second = sortDoubly(second) | ||
|
||
# Merge the two sorted halves | ||
return merge(tempHead, second) |
25 changes: 25 additions & 0 deletions
25
Coding Club India/Asked Goldman Sach Interview Questions/QuickSortDLL.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// CPP | ||
// Partition Function for QuickSort in Doubly Linked List | ||
// Asked in Goldman Sachs and HSBC Interview Questions | ||
|
||
struct Node partition(Node *l, Node *h){ | ||
int x = h->data; | ||
|
||
// similar to i = l-1 for array implementation | ||
Node *i = l->prev; | ||
|
||
// Similar to "for (int j = l; j <= h- 1; j++)" | ||
for (Node *j = l; j != h; j = j->next) | ||
{ | ||
if (j->data <= x) | ||
{ | ||
// Similar to i++ for array | ||
i = (i == NULL)? l : i->next; | ||
|
||
swap(&(i->data), &(j->data)); | ||
} | ||
} | ||
i = (i == NULL)? l : i->next; // Similar to i++ | ||
swap(&(i->data), &(h->data)); | ||
return i; | ||
} |
50 changes: 50 additions & 0 deletions
50
Coding Club India/Asked Microsoft Interview Questions/Add2NumberLL.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
def reverse(ll): | ||
curr = ll | ||
forward = prev = None | ||
while curr != None: | ||
forward = curr.next | ||
curr.next = prev | ||
prev = curr | ||
curr = forward | ||
ll = prev | ||
return ll | ||
|
||
def addLists(first, second): | ||
first = reverse(first) | ||
second = reverse(second) | ||
prev = None | ||
temp = None | ||
carry = 0 | ||
res = LinkedList() | ||
while(first is not None or second is not None): | ||
fdata = 0 if first is None else first.data | ||
sdata = 0 if second is None else second.data | ||
Sum = carry + fdata + sdata | ||
carry = 1 if Sum >= 10 else 0 | ||
Sum = Sum if Sum < 10 else Sum % 10 | ||
temp = Node(Sum) | ||
|
||
if res.head is None: | ||
res.head = temp | ||
else : | ||
prev.next = temp | ||
|
||
prev = temp | ||
|
||
if first is not None: | ||
first = first.next | ||
if second is not None: | ||
second = second.next | ||
|
||
if carry > 0: | ||
temp.next = Node(carry) | ||
|
||
res.head = reverse(res.head) | ||
return res.head | ||
|
||
# Input - 4 5, 3 4 5 || Output - 3 9 0 | ||
# Explanation - | ||
# 4->5 | ||
# 3->4->5 | ||
# ------- | ||
# 3->9->0 |
23 changes: 23 additions & 0 deletions
23
Coding Club India/Asked Microsoft Interview Questions/DeleteMidLL.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
''' | ||
class Node: | ||
def __init__(self, data): | ||
self.data = data | ||
self.next = None | ||
''' | ||
def deleteMid(head): | ||
''' | ||
head: head of given linkedList | ||
return: head of resultant llist | ||
''' | ||
if head == None or head.next == None: | ||
return None | ||
|
||
slow = fast = prev = head | ||
while slow and fast and fast.next: | ||
prev = slow | ||
slow = slow.next | ||
fast = fast.next.next | ||
|
||
prev.next = slow.next | ||
|
||
return head |
34 changes: 34 additions & 0 deletions
34
Coding Club India/Asked Microsoft Interview Questions/SortedInsertCircularLL.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
def sortedInsert(current, data): | ||
''' | ||
current: head of given sorted circular linked list | ||
data: data to be inserted | ||
return: head of resultant circular linked list | ||
''' | ||
new_node = Node(data) | ||
if current is None: | ||
new_node.next = new_node | ||
return new_node | ||
|
||
head = current | ||
if (current.data >= new_node.data): | ||
while current.next != head : | ||
current = current.next | ||
current.next = new_node | ||
new_node.next = head | ||
head = new_node | ||
return head | ||
|
||
else: | ||
while (current.next != head and | ||
current.next.data < new_node.data): | ||
current = current.next | ||
|
||
new_node.next = current.next | ||
current.next = new_node | ||
return head | ||
|
||
# Input - 1 2 4, 2 || Output - 1 2 2 4 | ||
# Explanation - | ||
# 1->2->4 :: Since 2 (to be inserted value) is 2<=2<4 | ||
# 1->2->2->4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def powerof4(n): | ||
if not n&3: | ||
return "True" | ||
return "False" | ||
|
||
if __name__ == "__main__": | ||
t = int(input()) | ||
for i in range(t): | ||
n = int(input()) | ||
print(powerof4(n)) |
36 changes: 36 additions & 0 deletions
36
Coding Club India/Interview Questions/Split2CircularLL.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
void splitList(Node *head, Node **head1_ref, Node **head2_ref) | ||
{ | ||
Node *slow_ptr = head; | ||
Node *fast_ptr = head; | ||
|
||
if(head == NULL) | ||
return; | ||
|
||
/* If there are odd nodes in the circular list then | ||
fast_ptr->next becomes head and for even nodes | ||
fast_ptr->next->next becomes head */ | ||
while(fast_ptr->next != head && | ||
fast_ptr->next->next != head) | ||
{ | ||
fast_ptr = fast_ptr->next->next; | ||
slow_ptr = slow_ptr->next; | ||
} | ||
|
||
/* If there are even elements in list | ||
then move fast_ptr */ | ||
if(fast_ptr->next->next == head) | ||
fast_ptr = fast_ptr->next; | ||
|
||
/* Set the head pointer of first half */ | ||
*head1_ref = head; | ||
|
||
/* Set the head pointer of second half */ | ||
if(head->next != head) | ||
*head2_ref = slow_ptr->next; | ||
|
||
/* Make second half circular */ | ||
fast_ptr->next = slow_ptr->next; | ||
|
||
/* Make first half circular */ | ||
slow_ptr->next = head; | ||
} |