首页 > 其他分享 >LeetCode: 328. Odd Even Linked List

LeetCode: 328. Odd Even Linked List

时间:2022-12-05 18:33:21浏览次数:38  
标签:Even even ListNode cur newList List Next odd Linked


LeetCode: 328. Odd Even Linked List

题目描述

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.

Example 1:

Input: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULL

Example 2:

Input: 2->1->3->5->6->4->7->NULL
Output: 2->3->6->7->1->5->4->NULL

Note:

  • The relative order inside both the even and odd groups should remain as it was in the input.
  • The first node is considered odd, the second node even and so on …

解题思路

  1. 用头插法生成奇数的逆序链表和偶数的逆序链表
  2. 用头插法按顺序将偶数链表和奇数链表插入新的链表中

AC 代码

/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func oddEvenList(head *ListNode) *ListNode {
var odd, even *ListNode

// split odd and even
iter := head
cur := head
for i := 1; iter != nil; i++ {
cur := iter
iter = iter.Next

if i % 2 == 1 {
cur.Next = odd
odd = cur
} else {
cur.Next = even
even = cur
}
}

// link odd and even
var newList *ListNode
for even != nil {
cur = even
even = even.Next

cur.Next = newList
newList = cur
}
for odd != nil {
cur = odd
odd = odd.Next

cur.Next = newList
newList = cur
}

return newList
}


标签:Even,even,ListNode,cur,newList,List,Next,odd,Linked
From: https://blog.51cto.com/u_15903085/5913229

相关文章

  • LeetCode: 234. Palindrome Linked List
    LeetCode:234.PalindromeLinkedList题目描述Givenasinglylinkedlist,determineifitisapalindrome.Example1:Input:1->2Output:falseExample2:Input:1->......
  • java并发数据结构之CopyOnWriteArrayList
    CopyOnWriteArrayList是一个线程安全的List实现,其在对对象进行读操作时,由于对象没有发生改变,因此不需要加锁,反之在对象进行增删等修改操作时,它会先复制一个对象副本,然后对......
  • DevExpress TreeList使用心得
    最近做项目新增光纤线路清查功能模块,思路和算法已经想好了,些代码时候居然在一个控件上纠结了好长的时间,虽然后来搞定了,但是好记性不然烂笔头,还是写下来,以后要用到的时候直接......
  • DevExpress TreeList 调优_绑定数据源方式, 放弃原来的AppendNode加载数据的方式
    注意事项1:由于一旦绑定了数据源dataTable的些许变化便在TreeList中有所体现,所以等dataTable完全填充好了之后再绑定数据源.注意事项2:dataTable每行的父节点ID当加载到......
  • virsh list报错: error: failed to connect to the hypervisor
    查看本地kvm虚机时,发现无法查看,出现报错#virshlisterror:failedtoconnecttothehypervisorerror:Failedtoconnectsocketto'/var/run/libvirt/libvirt-soc......
  • vba-传递listbox作为参数
    '反选PrivateSubCommandButton1_Click()CallTestss(Me!TIListBox)EndSubPrivateFunctionTestss(ByReflbAsObject)Iflb.ListCount<1ThenMsgBox......
  • 使用list和数组保存数据的差别
    在上位机开发曲线供能时遇到一个疑惑的问题,但又感觉这个问题太基础,想求证一下。需求:一共有1000个模拟量数据,每个数据记录600个点作为一组数据曲线,那么这1000个模拟量需要......
  • HACKTHEBOX VM LIST
    LinuxBoxes:WindowsBoxes:MorechallengingthanOSCP,butgoodpractice:LamelegacyJeeves[Windows]brainfuckBlueBart[Windows]shockerDevel......
  • vba-ArrayList
    TIListBox.MultiSelect=1TIListBox.ListStyle=1TIListBox.ColumnWidths=62TIListBox.ColumnCount=1Dimarr,brrC_Controlarr,b......
  • DevExpress 给TreeList添加右键菜单
    只有在右击节点时才会触发privatevoidtreeList1_MouseDown(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Right)......