class Node: def __init__(self): self.data = None # contains the data self.nextNode = None # contains the reference to the next node # create a linked list without a cycle head = Node() head.data = 0 current = head for i in range(1,9): new_node = Node() new_node.data = i current.nextNode = new_node current = new_node def printList(head): current = head while current.nextNode: print current.data current = current.nextNode print current.data def oddsEvens(head): temp = None current = head head2 = head.nextNode count = 1 while current.nextNode: count += 1 temp = current.nextNode if temp.nextNode: current.nextNode = temp.nextNode current = temp elif (count % 2 == 0): current.nextNode = head2 break else: current.nextNode = None temp.nextNode = head2 break printList(head) printList(head) oddsEvens(head)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question