Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6175022

Browse files
committed
streak day 1: swap nodes without data in linkedlist py
1 parent 0a86814 commit 6175022

File tree

3 files changed

+99
-5
lines changed

3 files changed

+99
-5
lines changed

‎README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 188 |
10-
| Current Daily Streak| 2 |
11-
| Last updated | 06/21/2019|
9+
| Total C++ Problems | 188 |
10+
| Total Python Problems | 5 |
11+
| Current Daily Streak| 1 |
12+
| Last Streak | 06/20/2019 - 06/21/2019|
13+
| Current Streak | 06/23/2019 - |
1214

1315
</center>
1416

@@ -19,7 +21,7 @@
1921
| :------------ | :----------: |
2022
| Find the nth node of linked list from last. |[nthToLastNode.cpp](linked_list_problems/nthToLastNode.cpp), [nth_to_last_node.py](linked_list_problems/nth_to_last_node.py) |
2123
| Add numbers where each digit of the number is represented by node of a linkedlist. Give output as a linked list. | [add_two_numbers_lists.cpp](linked_list_problems/add_two_numbers_lists.cpp), [add_two_numbers_list.py](linked_list_problems/add_two_numbers_lists.py)|
22-
| Swap nodes of a linkedlist without swapping data. |[swapNodesWithoutSwappingData.cpp](linked_list_problems/swapNodesWithoutSwappingData.cpp)|
24+
| Swap nodes of a linkedlist without swapping data. |[swapNodesWithoutSwappingData.cpp](linked_list_problems/swapNodesWithoutSwappingData.cpp), [swap_nodes_without_swapping_data.py](linked_list_problems/swap_nodes_without_swapping_data.py)|
2325
| Reverse a linked list, iteratively and recursively | [reverseLinkedListIterAndRecurse.cpp](linked_list_problems/reverseLinkedListIterAndRecurse.cpp)|
2426
| Given a linked list, reverse alternate nodes and append at the end. | [reverseAlternateNodes.cpp](linked_list_problems/reverseAlternateNodes.cpp) |
2527
| Only given a node pointer, delete the node from the linked list. | [deleteNode.cpp](linked_list_problems/deleteNode.cpp)|

‎linked_list_problems/swapNodesWithoutSwappingData.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void swapSpecial(Node* & head, int x, int y) {
6060
if ( prevy != nullptr) {
6161
prevy->next = currx;
6262
} else {
63-
head = curry;
63+
head = currx;
6464
}
6565

6666
//now lets swap the next pointers
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
# Problem : Given a linked list, and two data values, x and y.
3+
# Goal: Swap the nodes of linkedlist by swapping pointers
4+
# (and not swapping data) such that nodes with data x will contain y and vice versa.
5+
6+
7+
class Node:
8+
"""Representation of a linked list node"""
9+
def __init__(self, data):
10+
self.data = data
11+
self.next = None
12+
13+
14+
class LinkedList:
15+
"""Representation of a linked list"""
16+
def __init__(self):
17+
self.head = None
18+
19+
def push(self, data):
20+
"""Pushes new data to the head of the list"""
21+
new_node = Node(data)
22+
new_node.next = self.head
23+
self.head = new_node
24+
25+
def print_list(self):
26+
"""Prints a linked list"""
27+
itr = self.head
28+
while itr:
29+
if itr.next is None:
30+
print(itr.data)
31+
else:
32+
print(str(itr.data) + '-->', end = ' ')
33+
itr = itr.next
34+
35+
def swap_nodes_without_swapping_data(self, x, y):
36+
"""Swap the nodes of linked without swapping pointers i.e.
37+
swap nodes such that nodes with data x will contain y and vice versa."""
38+
39+
# We are making assumption that nodes in linked list are unique
40+
if x == y:
41+
return
42+
43+
curr_x = self.head
44+
curr_y = self.head
45+
prev_x = None
46+
prev_y = None
47+
48+
# find node with data x
49+
while curr_x and curr_x.data != x:
50+
prev_x = curr_x
51+
curr_x = curr_x.next
52+
53+
# find node with data y
54+
while curr_y and curr_y.data != y:
55+
prev_y = curr_y
56+
curr_y = curr_y.next
57+
58+
# x or y is not there
59+
if curr_x is None or curr_y is None:
60+
return
61+
62+
# if x is not head of the linked list.
63+
if prev_x is not None:
64+
prev_x.next = curr_y
65+
else: # if x was head, make y the head of the linked list.
66+
self.head = curr_y
67+
68+
# if y is not head of the linked list
69+
if prev_y is not None:
70+
prev_y.next = curr_x
71+
else: # if y was head, make x the head of the linked list.
72+
self.head = curr_x
73+
74+
# now swap the next pointers.
75+
temp = curr_x.next
76+
curr_x.next = curr_y.next
77+
curr_y.next = temp
78+
79+
if __name__ == "__main__":
80+
lst = LinkedList()
81+
lst.push(5)
82+
lst.push(1)
83+
lst.push(3)
84+
lst.push(2)
85+
lst.push(4)
86+
87+
print("List before:")
88+
lst.print_list()
89+
90+
lst.swap_nodes_without_swapping_data(1, 4)
91+
print("List after:")
92+
lst.print_list()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /