Skip to content

Commit

Permalink
Create Swap_Nodes_Linkedlist.py
Browse files Browse the repository at this point in the history
Adjescent nodes swapping algorithm for linked list.
  • Loading branch information
guptaarth87 authored Oct 15, 2022
1 parent 92f4079 commit 8425a65
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions data_structures/Linked_list/Python/Swap_Nodes_Linkedlist.py
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



0 comments on commit 8425a65

Please sign in to comment.