ARTS Week 27

KeepNewbie_yan
3 min readFeb 27, 2022

Algorithm

This week's LeetCode problem is 328. Odd Even Linked List

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.

First, separate the odd and even head nodes, then traverse the links to find the odd node sub-linked list and the even node sub-linked list respectively, and finally create a new linked list header to connect the odd and even linked lists respectively.

Input: head = [1,2,3,4,5]
Output: [1,3,5,2,4]
/**
* 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 oddEvenList(ListNode head) {
if (head == null || head.next == null || head.next.next == null) {
return head;
}
ListNode oddDummy = head, oddNode = head;
ListNode evenDummy = head.next, evenNode = head.next;
while (oddNode != null && evenNode != null) {
oddNode.next = evenNode.next;
oddNode = oddNode.next;
if (oddNode != null) {
evenNode.next = oddNode.next;
evenNode = evenNode.next;
}
}
oddNode = oddDummy;
while (oddNode.next != null) {
oddNode = oddNode.next;
}
oddNode.next = evenDummy;
return oddDummy;
}
}

Review

This week's Review is for the following article: What is port forwarding?

Port forwarding is to send network traffic from one port to another port on the same computer or on a different computer. After a specific port receives traffic, it will be processed by a specific network application, such as port 80 to handle website requests. This article explains how to do port forwarding:

  1. Port forwarding with your router, by setting the external port to 1234 and the internal port to 30000, this will forward port 1234 traffic to port 30000 on 10.0.1.2.
  2. Port forwarding with a firewall, you need to use the firewall-cmd command to forward traffic on the server:
$ sudo firewall-cmd \
--add-forward-port \
port=80:proto=tcp:toport=8065

To make changes permanent, add the --runtime-to-permanent option.

3. Beyond that, for example using IP forwarding or setting up a proxy.

Tip

How to call C language functions in Python? There is a ctypes library in Python, which can call functions in dynamic link libraries or shared libraries. Because C language supports function overloading, in order to avoid conflicts in binary, extern is required for modification. Here is an example:

//lib.cpp
#include <iostream>

int Function(int num)
{
std::cout << "Num = " << num << std::endl;
return 0;
}

extern "C" {
int My_Function(int a)
{
return Function(a);
}
}

Compile the above code into a shared library using the following compile command:

g++ -fPIC -shared -o libTest.so lib.cpp

The code for Python to call the shared library is as follows:

import ctypes
import sys
import os

dir_path = os.path.dirname(os.path.realpath(__file__))
handle = ctypes.CDLL(dir_path + "/libTest.so")

handle.My_Function.argtypes = [ctypes.c_int]

def My_Function(num):
return handle.My_Function(num)

The Python test code is as follows:

from myLib import *

My_Function(16)

The results are as follows:

$ python3 test.py
Num = 16

Share

Life has gradually returned to the right track. In the past week, I have basically gone out for a brisk walk or run every day to exercise, and I feel that no matter what the situation is, the body will always come first.

The Python test code is as follows:

from myLib import *My_Function(16)

The results are as follows:

$ python3 test.py
Num = 16

Share

Life has gradually returned to the right track. In the past week, I have basically gone out for a brisk walk or run every day to exercise, and I feel that no matter what the situation is, the body will always come first.

--

--

KeepNewbie_yan

A programmer. Share knowledge of programming, operating system, Linux kernel, and reading, thinking etc. Let us maintain a beginner mend and grow together!