Skip to content

Latest commit

 

History

History
39 lines (31 loc) · 1.06 KB

328.md

File metadata and controls

39 lines (31 loc) · 1.06 KB

题解

思路 1、利用数组下标

代码

# Definition for singly-linked list.  
# class ListNode:  
#     def __init__(self, val=0, next=None):  
#         self.val = val  
#         self.next = next  
class Solution:  
    def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:  
        first = ListNode()  
        second = ListNode()  
        first_pointer = first  
        second_pointer = second  
        i = 1  
        while head != None:  
            if i % 2 == 1:  
                first_pointer.next = ListNode(head.val)  
                first_pointer = first_pointer.next  
            else:  
                second_pointer.next = ListNode(head.val)  
                second_pointer = second_pointer.next  
            head = head.next  
            i += 1  
        first_pointer.next = second.next  
        return first.next

思路 2,原链表上操作