-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjescent nodes swapping algorithm for linked list.
- Loading branch information
1 parent
92f4079
commit 8425a65
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
data_structures/Linked_list/Python/Swap_Nodes_Linkedlist.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,36 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
if head==None: | ||
return None | ||
if head.next==None: | ||
return head | ||
tmp1=head | ||
tmp2=head.next | ||
while tmp1: | ||
|
||
if tmp1 !=None and tmp2 !=None: | ||
print(tmp1.val) | ||
print(tmp2.val) | ||
|
||
tmpx_val=tmp1.val | ||
tmpx=tmp1 | ||
|
||
tmp1.val=tmp2.val | ||
tmp2.val=tmpx_val | ||
|
||
tmp1=tmp2.next | ||
if tmp2.next!=None: | ||
tmp2=tmp2.next.next | ||
else: | ||
temp2=None | ||
else: | ||
break | ||
return head | ||
|
||
|
||
|