Jerry's Blog

Recording what I learned everyday

View on GitHub


2 September 2019

Vim(2)

by Jerry Zhang

LeetCode Day 34: P206. Reverse Linked List (Easy)

题目

反转一个单向链表。

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

我的思路

依次修改箭头方向。

答案

public class E_206_ReverseLinkedList {
    public ListNode reverseList(ListNode head) {
        ListNode current = head;
        ListNode pre = null;
        while(current != null) {
            current = current.next;
            head.next = pre;
            pre = head;
            head = current;
        }
        return pre;
    }
}

Vim CRUD

Insert

a/i/o, A/I/O

Delete in normal mode

Change

r(replace), c(change), s(substitute)

Search and Replace

:[range]s[ubstitute]/{pattern}/{string}/[flags]

10, 20 means 10-20 lines; % means all

For example: :% s/abc/def/g Substitute all “abc” to “def”.

:1,3 s/abc/def/g Substitute all “abc” to “def” between line 1 and line 3 include.

:1,5 s/abc//n Counting number of a certain word. Count how many “abc” from line 1 to line 5. (not working in the vscode vim, but it works in the linux vi)

:% s/\<abc\>/def/g Only substitute the word “abc” without anything around.

tags: Vim