Skip to content

Commit

Permalink
Odd Even Linked List
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Sep 16, 2018
1 parent 62c1c65 commit 1b01a47
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lc328.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None


class Solution:
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
odd = head
if not head.next:
return head
evenhead = even = head.next

while even:
odd.next = even.next
if odd.next:
odd = odd.next
even.next = odd.next
even = even.next

odd.next = evenhead
return head


def test(s):

g = (int(x) for x in s.split('->'))

head = ListNode(0)
tail = head
for i in g:
tail.next = node = ListNode(i)
tail = tail.next
soln=Solution()
soln.oddEvenList(head.next)
while(head):
print(head.val)
head = head.next


s = '1->2->3->4->5'
test(s)

0 comments on commit 1b01a47

Please sign in to comment.