Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
The first node is considered odd, and the second node is even, and so on.
Note that the relative order inside both the even and odd groups should remain as it was in the input.
You must solve the problem in O(1) extra space complexity and O(n) time complexity.
Example 1:
Input: head = [1,2,3,4,5]
Output: [1,3,5,2,4]
Example 2:
Input: head = [2,1,3,5,6,4,7]
Output: [2,3,6,7,1,5,4]
Constraints:
func oddEvenList(head *ListNode) *ListNode {
var odd *ListNode
var even *ListNode
var e *ListNode
if head != nil {
odd = head
even, e = head.Next, head.Next
} else {
return head
}
for odd != nil && even != nil && odd.Next != nil && even.Next != nil {
odd.Next = even.Next
odd = odd.Next
even.Next = odd.Next
even = even.Next
}
odd.Next = e
return head
}
https://go.dev/play/p/_laRQXpqGBH
func oddEvenList(head *ListNode) *ListNode {
if head ==nil {return nil}
odd := head
even := head.Next
evenHead := head.Next
for even!=nil && even.Next!=nil {
// put odd to odd list
odd.Next = odd.Next.Next
// put even to even list
even.Next = even.Next.Next
// move to next for both odd and even
odd = odd.Next
even = even.Next
}
// put even after odd
odd.Next = evenHead
return head
}
func oddEvenList(head *ListNode) *ListNode {
if head == nil {
return head
}
a := head
b := head.Next
es := b
for b != nil && b.Next != nil {
a.Next = b.Next
b.Next = b.Next.Next
if a.Next != nil {
a = a.Next
}
b = b.Next
}
a.Next = es
return head
}
func oddEvenList(head *ListNode) *ListNode {
if head == nil {
return nil
}
var odd, even = head, head.Next
var evenHead = even
for even != nil && even.Next != nil {
odd.Next = even.Next
odd = odd.Next
even.Next = odd.Next
even = even.Next
}
// Append all even list at the end of odd list
odd.Next = evenHead
return head
}
func oddEvenList(head *ListNode) *ListNode {
if head == nil {
return head
}
odd := head
even := head.Next
evenHead := even // just to keep track of where even-node list starts
for even != nil && even.Next != nil {
odd.Next = odd.Next.Next
odd = odd.Next
even.Next = even.Next.Next
even = even.Next
}
odd.Next = evenHead
return head
}
func oddEvenList(head *ListNode) *ListNode {
var eHead, oHead *ListNode
var odds, evens *ListNode = &ListNode{Val: -1}, &ListNode{Val: -1}
idx := 0
for head != nil {
if idx%2 == 0 {
//even
if eHead == nil {
eHead = head
}
evens.Next, evens = head, head
} else {
if oHead == nil {
oHead = head
}
odds.Next, odds = head, head
}
head = head.Next
idx++
}
//merge the two list
evens.Next, odds.Next = oHead, nil
return eHead
}
func oddEvenList(head *ListNode) *ListNode {
if head == nil {
return nil
}
var headEven *ListNode = head.Next
even := head.Next
odd := head
for (even != nil && even.Next != nil) {
odd.Next = odd.Next.Next
even.Next = even.Next.Next
even = even.Next
odd = odd.Next
}
odd.Next = headEven
return head
}
func oddEvenList(head *ListNode) *ListNode {
if head == nil {
return nil
}
var even *ListNode = &ListNode{Next: head}
odd := head
// even := head.Next
curEven := even
for (odd != nil && odd.Next != nil) {
even.Next = even.Next.Next
odd.Next = odd.Next.Next
even = even.Next
odd = odd.Next
}
even.Next = nil
curOdd := head
for(curOdd.Next != nil) {
curOdd = curOdd.Next
}
curOdd.Next = curEven.Next
return head
}
func oddEvenList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
odd := head
even := head.Next
evenHead := head.Next
for even != nil && even.Next != nil {
odd.Next = even.Next
odd = even.Next
even.Next = odd.Next
even = even.Next
}
odd.Next = evenHead
return head
}
[Go] Linked list - Doubly Linked List from java to GO (0) | 2022.05.09 |
---|---|
[Go] Linked List - Palindrome Linked List (0) | 2022.05.09 |
[Go] Linked List - Remove Linked List Elements (0) | 2022.05.05 |
[Go] Linked List - Reverse Linked List (0) | 2022.05.02 |
[Go] Linked List - Summary - Two-Pointer in Linked List from Java to Go (0) | 2022.05.01 |
댓글 영역