Maximum Odd Binary Number - LeetCode
Can you solve this real interview question? Maximum Odd Binary Number - You are given a binary string s that contains at least one '1'. You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can
leetcode.com
풀이
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode prev = head;
ListNode after = head;
ListNode cntNode = head;
int cnt = 0;
while(cntNode != null) {
cnt += 1;
cntNode = cntNode.next;
}
n = cnt - n;
if(n == 0) {
return head.next;
}
for(int i = 0; i <= n; i++) {
after = after.next;
}
for(int i = 0; i <= n-2; i++) {
prev = prev.next;
}
prev.next = after;
return head;
}
}
'알고리즘 > 릿코드' 카테고리의 다른 글
[Leetcode] - Maximum Odd Binary Number (0) | 2024.03.02 |
---|---|
[Leetcode] Jump Game IV (0) | 2023.03.05 |