Chain List Flip Series

Source: Internet
Author: User

Problem I

Flip Linked list

Program (compact version)

Non-recursive

ListNode reverselist (ListNode head) {ListNode pre = NULL; ListNode cur = head;while (cur! = null) {ListNode next = Cur.next;cur.next = Pre;pre = Cur;cur = Next;} return pre;}

Problem Ii:swap Nodes in Pairs

Given a linked list, swap every, adjacent nodes and return its head.

For example,
Given 1->2->3->4 , you should return the list as 2->1->4->3 .

Your algorithm should use only constant space. Modify the values in the list, only nodes itself can be changed.

Swap a pair of nodes in the linked list in turn.

Program

public class Solution {public    ListNode swappairs (ListNode head) {ListNode dummy = new ListNode (-1); ListNode pre = head; ListNode connect = Dummy;while (pre! = null && Pre.next! = null) {ListNode next = Pre.next; ListNode cross = Next.next;next.next = Pre;pre.next = Cross;connect.next = Next;connect = Pre;pre = Cross;} return dummy.next = = null? Head:dummy.next;}}

Problem Iii:reverse Nodes in K-group

Given A linked list, reverse the nodes of a linked list K at a time and return its modified list.

If the number of nodes is not a multiple of K then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, and only nodes itself is changed.

Only constant memory is allowed.

For example,
Given This linked list:1->2->3->4->5

For k = 2, you should return:2->1->4->3->5

For k = 3, you should return:3->2->1->4->5

Program

public class Solution {public ListNode Reversekgroup (ListNode head, int k) {if (head = = NULL | | head.next = = N ull | |        K <= 1) {return head;        } ListNode dummy = new ListNode (-1);        Dummy.next = head;        ListNode pre = dummy;        ListNode cur = head;                int cnt = 0;            while (cur! = null) {++cnt;                if (cnt% k = = 0) {//reverse group ListNode last = Reversegroup (pre, cur.next);                Pre = last;            cur = last.next;            } else {cur = cur.next;    }} return dummy.next;        } private ListNode Reversegroup (ListNode Pre, ListNode next) {ListNode last = Pre.next;                ListNode cur = last.next;            while (cur! = next) {Last.next = Cur.next;            Cur.next = Pre.next;            Pre.next = cur;        cur = last.next; } return LAst }}

The difficulty is that each time a node is flipped between two nodes.

Problem Iv:reverse LinkedList II

Reverse a linked list from position m to N. Do it in-place and in One-pass.

For example:
Given 1->2->3->4->5->NULL , m = 2 and n = 4,

Return 1->4->3->2->5->NULL .

Note:
Given m, n satisfy the following condition:
1 ≤ mn ≤length of list.

Program

public class Solution {public    ListNode Reversebetween (listnode head, int m, int n) {        if (head = = NULL | | head.nex T = = null) {            return head;        }        ListNode dummy = new ListNode (0);        Dummy.next = head;        ListNode pre = dummy;                while (--m > 0) {            pre = Pre.next;        }                ListNode next = head;        while (--n > 0) {            next = Next.next;        }                Reversebetween (pre, next.next);        return dummy.next;    }        private void Reversebetween (ListNode Pre, ListNode next) {        ListNode last = Pre.next;        ListNode cur = last.next;                while (cur! = next) {            last.next = Cur.next;            Cur.next = Pre.next;            Pre.next = cur;            cur = last.next;}}    }

Find the right location, apply the method of question 3 ~

Chain List Flip Series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.