import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class ListNode { public int val; public ListNode next; public ListNode(int val) { this.val = val; this.next = null; } } public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] list = br.readLine().trim().split(" "); ListNode head = new ListNode(Integer.parseInt(list[0])); ListNode cur = head; for(int i = 1; i < n; i++){ cur.next = new ListNode(Integer.parseInt(list[i])); cur = cur.next; } String[] params = br.readLine().split(" "); int start = Integer.parseInt(params[0]); int end = Integer.parseInt(params[1]); head = reversePartLinkedList(head, start, end); while(head != null){ System.out.print(head.val + " "); head = head.next; } } private static ListNode reversePartLinkedList(ListNode head, int start, int end) { ListNode dummy = new ListNode(-1); dummy.next = head; ListNode prev = dummy; for(int i = 1; i < start; i++) prev = prev.next; ListNode cur = prev.next; // 开始翻转的链表头结点 for(int i = start; i < end; i++){ ListNode temp = cur.next; cur.next = temp.next; temp.next = prev.next; prev.next = temp; } return dummy.next; } }
list_node * reverse_list(list_node * head, int L, int R)
{
//////在下面完成代码
if(head == nullptr || R < L || L < 1)
return head;
list_node * p = head;
list_node * pre = nullptr;//
list_node * pos = nullptr;
int len = 0;
while(p){
len++;
pre = (len == L -1 ? p : pre);
pos = (len == R + 1 ? p : pos);
p = p->next;
}
if(R > len)
return head;
list_node *node1 = (pre == nullptr ? head : pre->next);
list_node *node2 = node1->next;
node1->next = pos;
list_node *next = nullptr;
while(node2 != pos){
next = node2->next;
node2->next = node1;
node1 = node2;
node2 = next;
}
if(pre != nullptr){
pre->next = node1;
return head;
}
return node1;
}
list_node * reverse_list2(list_node *head, list_node *end, int l){ list_node *pre = end, *p = head, *q = head->next; while(l--){ p->next = pre; pre = p; p = q; q = p->next; } return pre; } list_node * reverse_list(list_node * head, int L, int R) { list_node * rhead = new list_node(); rhead->next = head; list_node *p = rhead; for(int i=0;i<L-1;i++) p = p->next; list_node *q = p->next; for(int i=L;i<R+1;i++) q = q->next; p->next = reverse_list2(p->next, q, R-L+1); return rhead->next; }
class ListNode{ public int val; public ListNode next; public ListNode(int val){ this.val = val; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int[] val = new int[length]; for (int i = 0; i < length; i++) { val[i] = scanner.nextInt(); } int left = scanner.nextInt(); int right = scanner.nextInt(); ListNode head = new ListNode(val[0]); ListNode cur = head; for (int i = 1; i < length; i++) { cur.next = new ListNode(val[i]); cur = cur.next; } head = reversePartLinkedList(head,left,right); while (head != null){ System.out.print(head.val + " "); head = head.next; } } private static ListNode reversePartLinkedList(ListNode head, int start, int end) { ListNode dummy = new ListNode(-1); dummy.next = head; ListNode prev = dummy; for (int i = 1;i < start;i++){ prev = prev.next; } ListNode prev2 = prev.next; ListNode prev3 = prev.next; ListNode cur = prev2.next; for (int i = start; i < end; i++) { ListNode curNext = cur.next; cur.next = prev2; prev2 = cur; cur = curNext; prev3.next = curNext; } prev.next = prev2; return dummy.next; } }
import java.util.Scanner; /* * 反转部分单向链表 */ class Node { int val; Node next; public Node(int val) {//构造方法 super(); this.val = val; this.next = null; } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int len = sc.nextInt(); int []value = new int[len]; for (int i = 0; i < len; i++) { value[i] = sc.nextInt(); } int left = sc.nextInt(); int right = sc.nextInt(); reverse( value, left-1, right-1); Node head = arrayToList(value); Node node; for (node = head; node.next != null; node = node.next) { System.out.print(node.val + " "); } System.out.print(node.val); } public static void reverse(int []value, int left, int right) { while (left < right) { int tmp = value[left]; value[left] = value[right]; value[right] = tmp; left++; right--; } } public static Node arrayToList(int []value) { Node head = new Node(-1); Node node = head; for (int i = 0; i < value.length; i++) { Node newNode = new Node(value[i]); node.next = newNode; node = newNode; } return head.next; } }
import java.util.*; import java.io.*; class ListNode{ public int val; public ListNode next; public ListNode(int val){ this.val = val; this.next = null; } } public class Main{ public static ListNode ReverseList(ListNode Head,int L,int R){ ListNode pHead = new ListNode(-1); pHead.next = Head; ListNode prevNode = pHead; for(int i = 0; i < L-1; i++){ prevNode = prevNode.next; } ListNode cur = prevNode.next; for(int i = 0; i < R-L; i++){ //先保存cur的下一个结点 ListNode curNext = cur.next; //删除下一个结点,cur指向curNext的下一个结点 cur.next = curNext.next; //删除的结点头插到prevNode的后面 curNext.next = prevNode.next; prevNode.next = curNext; } ListNode list = pHead.next; return list; } public static void main(String[] args) throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String str; while((str = reader.readLine()) != null){ int n = Integer.parseInt(str); str = reader.readLine(); String[] list = str.trim().split(" "); ListNode head = new ListNode(Integer.parseInt(list[0])); ListNode cur = head; for(int i = 1; i < n; i++){ cur.next = new ListNode(Integer.parseInt(list[i])); cur = cur.next; } str = reader.readLine(); String[] params = str.trim().split(" "); int L = Integer.parseInt(params[0]); int R = Integer.parseInt(params[1]); ListNode Head = ReverseList(head,L,R); while(Head != null){ System.out.print(Head.val + " "); Head = Head.next; } } } }
import java.util.Scanner; /** * 反转部分单向链表 * @author haomin * @date 2022/05/26 18:51 **/ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); Node head = new Node(-1); Node cur = head; // 1. 得到题给的链表 for (int i = 0; i < num; i++) { Node node = new Node(in.nextInt()); cur.next = node; cur = cur.next; } int leftIndex = in.nextInt(); int rightIndex = in.nextInt(); // 2. 记录left左边第一个节点 Node frontNode = head; // 3. 记录right右边第一个节点 Node hinderNode = head; // 2.1 for (int i = 0; i < leftIndex-1; i++) { frontNode = frontNode.next; } // 得到开始进行反转的节点 Node firstNode = frontNode.next; // 3.1 for (int i = 0; i <= rightIndex; i++) { hinderNode = hinderNode.next; } // 4. 反转指定部分链表 // 连接right后面的节点 Node newNode = hinderNode; int count = rightIndex - leftIndex + 1; while (count-- != 0){ Node pre = new Node(firstNode.val); pre.next = newNode; newNode = pre; firstNode = firstNode.next; } // 连接left前面的节点 frontNode.next = newNode; // 5. 打印 head = head.next; while (head != null){ System.out.print(head.val+" "); head = head.next; } } } class Node{ int val; Node next; public Node(int val) { this.val = val; } }
let n=parseInt(readline()) let nums=readline().split(' ').map(item=>parseInt(item)) let [L,R]=readline().split(' ').map(item=>parseInt(item)) const listNode=function(val){ this.val=val this.next=null } const head=new listNode(-1) let p=head for(let i=0;i<n;i++){ const node=new listNode(nums[i]) p.next=node p=node } let q=head let pre=null,last=null p=head.next let count=R-L let flag=0 while(L!=0||R!=0){ L-- R-- if(L==0){ pre=q flag++ } if(R==0){ last=p flag++ } if(flag==2) break q=q.next p=p.next if(p==null) break } if(p==null){ last=q } while(pre.next!=last){ let cur=pre.next pre.next=cur.next cur.next=last.next last.next=cur } p=head.next let res=[] while(p){ res.push(p.val) p=p.next } console.log(res.join(' '))
list_node * reverse_list(list_node * head, int L, int R) { //////在下面完成代码 list_node *pre,*cur,*tail,*hs,*t; pre = t = hs = head; if(head->next == NULL) return head; for(int i = 1; i < R; i++){ if(i < L){ if(i == L-1){ hs = pre;//hs为L的前一个节点 t = pre->next;//t为第L个节点 } pre = pre->next; } else{ if(i == L) cur = pre->next; tail = cur->next; cur->next = pre; pre = cur; cur = tail; } } // 如果L=1(此时t == head),第pre个节点就成了第一个节点否则head为第一个节点 // 也可以创造一个头结点,然后再进行反转,这样就无需考虑L的情况。 // 例如 // 反转1到3,1 2 3 4 5,pre=3成了第一个节点 // 反转2到4,1 2 3 4 5变成了1 4 3 2 5,pre=4并不是第一个节点,head才是。 t->next = tail; if(t == head) return pre; else{ hs->next = pre; return head; } }
list_node* reverse_list(list_node* head, list_node * last, int k) { list_node* prev = last; list_node* cur = head; while(k--) { list_node* next = cur->next; cur->next = prev; prev = cur; cur = next; } return prev; } list_node * reverse_list(list_node * head, int L, int R) { //////在下面完成代码 list_node* dummy = new list_node(); dummy->next = head; list_node* p = dummy; for(int i = 0; i < L-1; ++i) { p = p->next; } list_node* q = dummy; for(int i = 0; i < R+1; ++i) { q = q->next; } p->next = reverse_list(p->next, q, R-L+1); list_node* ret = dummy->next; dummy->next = NULL; delete dummy; return ret; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static class Node { public int value; public Node next; public Node(int value) { this.value = value; } } public static Node reversePart(Node head, int from, int to) { Node node1 = head; Node begin = null; Node end = null; int len = 0; while (node1 != null) { len++; // 找到反转部分的前一个结点 begin = len == from - 1 ? node1 : begin; // 找到反转部分的后一个结点 end = len == to + 1 ? node1 : end; node1 = node1.next; } if (from > to || from < 1 || to > len) { return head; } node1 = begin == null ? head : begin.next; Node node2 = node1.next; node1.next = end; Node next = null; while (node2 != end) { next = node2.next; node2.next = node1; node1 = node2; node2 = next; } if (begin != null) { begin.next = node1; return head; } else { return node1; } } public static void printList(Node head) { while (head != null) { System.out.print(head.value +" "); head = head.next; } System.out.println(); } public static Node listGenerator(int length, String[] numbers) { Node head = new Node(Integer.parseInt(numbers[0])); Node cur = head; for (int i = 1; i < length; i++) { cur.next = new Node(Integer.parseInt(numbers[i])); cur = cur.next; } cur.next = null; return head; } public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bufferedReader.readLine()); String[] numbers = bufferedReader.readLine().split(" "); String[] parameters = bufferedReader.readLine().split(" "); int L = Integer.parseInt(parameters[0]); int R = Integer.parseInt(parameters[1]); Node head = listGenerator(n, numbers); head = reversePart(head, L, R); printList(head); } }